How to Run PHP Program

1.Using $_GET

PHP allows you to interact with your visitors and handle incoming data. This means that you can use either the URL (querystring) or forms to retrieve user input. There are additional ways to access data as well, but we will not be covering those in this introduction.

You’ll notice when you visit this page nothing happens – so let’s change that! To access the value of name in the querystring, we can use $_GET[‘name’] like so:

<html>

<head>

<title>Hello</title>

</head>

<body>

Hello <?php echo $_GET[‘name’]

</body>

</html>

You’ll notice that unlike the text “there!” that the GET is not in quotes – this is because this is a variable and by not placing it in quotes we’re telling PHP to render this as a variable and not as text. If we leave the single quotes, instead of saying “Hello yourname” it would say “Hello $_GET[‘name’].”

2.Using logic and defining variables

Along with getting user input, you can also create conditions to determine what content should be output. For example, we can determine whether or not to say “Good morning” or “Good evening” depending on the time, along with your name using the querystring.

To do this, we’ll be using if, elseif, and else along with the PHP date() function. You can learn more about how to use different date formats to output the date here, but we’ll be using the date() function to get back the hour of the day (based on the server’s time) between 0 (midnight) and 23 (11pm). We’ll then use greater than (>) to determine what to assign to our $time variable which we’ll output with the user’s name.

<html>

<head>

<title>Hello</title>

</head>

<body>

<?php

if(date(“G”) > 18) {

$time = ‘evening’;

} elseif (date(“G”) > 12) {

$time = ‘afternoon’;

} else {

$time = ‘morning’;

}

echo ‘Good ‘.$time.’ ‘.$_GET[‘name’];

?>

</body>

</html>

Now upload your script again to the web server and refresh the page. Depending on the time of the server you should see either Good morning, Good afternoon, or Good evening followed by your name.

If you get an error, or the page is blank, make sure you have closed all of your quotes and have a semicolon after your statements/ commands. Missing a quote or semicolon is one of the most common causes of PHP errors.

You may also receive an error if the timezone has not been set on your server. To resolve this (or change the timezone/ output of the script) try adding this line as the first line following the opening PHP bracket (<?php):

date_default_timezone_set(‘America/Los_Angeles’);

With that you have created your first PHP script and have already taken advantage of many of the fundamentals used in every PHP program. While there is more to learn you are well on your way, and have a great start on defining variables, using user input, and taking advantage of PHP’s built in functions.

3.How to run php program

XAMPP is a Web development tool, created by Apache, that makes it easy to run PHP (Personal Home Pages) scripts on your computer locally. Installation of XAMPP Server on windows is easy as compared to manual installation of a web server and PHP required a lot of in-depth configuration knowledge. XAMPP package installs MySQL, FileZilla, Mercury, Perl, and Tomcat along with Web Server and PHP, with these applications you can test the full website on your desktop. You don’t need to upload it every time on an online Web server.

Step 1: First of all, open the Apache Friends website and download XAMPP for Windows, and install it.

Step 2: Start the XAMPP Program Control Panel. Click on the “Start”  button next to the “Apache” to start your Apache Web Server. Also, start “MySQL” if your PHP programs depend on a MySQL database to run.

Step 3: Place your PHP files in the “htdocs” folder located under the “XAMPP” folder on your drive (i.e. C/D/E etc). The initial path is “your_drive_letter:\xampp\htdocs” for your Web server. Make sure that your PHP files are saved as a “.php” file extension.

Example: The “demo.php” file is saved in the htdocs folder.

<!DOCTYPE html>

<html>

<body>

  <h1>Hello GFG </h1>

  <?php

    echo “Hello hitech”;

  ?>

</body>

</html>

Step 4: Open up any web browser and enter “localhost/filename”. This will open the list of all the files and folders stored under the “htdocs” folder on your computer. Click on the link to a PHP file and open it to run a program.

Example: The file “demo.php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo.php” and press enter. Your program will run.

Syntax: php have special Syntax to write a code, as shown in below 

<?php   
//write your code here  
?>  

demo.php

<!DOCTYPE html>

<html>

<body>

  <h1>Hello All </h1>

  <?php

    echo “hello Hitech”;

   ?>

</body>

</html>

Step 5: You can create any folder to test PHP files under the “htdocs” folder. If you create a specific folder then you need to use the address as “localhost/foldername” to open them in your browser.

Example: The “demo.php” file is placed inside the “kalai” folder. Enter “localhost/kalai/demo.php” in your browser and press enter, your program will be run.

PHP case sensitivity

PHP is case sensitive language, in that variables and functions, tags are case sensitive but classes are not case sensitive. Below the example of case sensitivity of php

Example : color.php

<html>

<body>

<?php

$color = “black”;

echo “My car is “. $ColoR .”</br>”;

echo “My dog is “. $color .”</br>”;

echo “My Phone is “. $COLOR .”</br>”;

?>

</body>

</html>

In the above example, the variable name is case sensitive, so it gives error.

Output:

My car is 

My dog is black

My Phone is 

PHP Notice:  Undefined variable: ColoR in color.php on line 3

PHP Notice:  Undefined variable: COLOR in color.php on line 5

Leave a Comment

Your email address will not be published. Required fields are marked *