Introduction to PHP – Part 1
PHP is a widely used open source server side scripting language that is used to create dynamic and interactive web pages. PHP can be directly embedded into the HTML code and hence, is perfect for any web development. The syntax of PHP is very similar to Perl and C. PHP works on Apache web server and also supports Microsoft Internet Information Server.
If you want to use PHP for website and web applications you need three things; PHP itself, a web server and web browser. To design and develop web pages you need a web development application. In addition, if you want to create dynamic web pages, you might also need database functionalities.
I suggest you to install WampServer and Macromedia Dreamweaver. WampServer is a Windows web development environment that comes with Apache webserver, PHP and MySQL database. You can download WampServer for free using this link http://www.wampserver.com/en/download.php. For more information on how to install WampServer visit this link, http://www.wampserver.com/en/presentation.php.
PHP block starts with and can be placed anywhere in HTML code. Each line of code ends with semicolon. Semicolon is used to separate one set of instructions from another. The example below is a simple PHP script which sends the message “Hello World” to the browser.
Create a file and save it with name example1.php and paste the following code. This code is extremely simple, contains HTML tags and a block of PHP script. The highlighted portion of the code is PHP code and is embedded into HTML. The whole file needs to be saved with .php extension, if the file has other than .php extension like .html, the PHP script will not execute.
The file can be accessed using browser with web server’s URL, ending with the /example1.php file reference. For example if you are developing locally, the URL will look like http://localhost/example1.php.
<html> <head> <title>First Example</title> </head> <body> <?php echo "Hello World"; ?> </body> </html>
For comments, // or # is used for single line comment and /* and */ is used for multi line comments. Now amend the file as follows. The output will remains the same, why?.
<html> <head> <title>Example with comments</title> </head> <body> <?php //Single line comment or #Single line comment echo"Hello World"; /* Multi line comment Line 1 Line 2 ... */ ?> </body> </html>
Normally, most of the programming languages are strongly typed and require the data type while declaring variables but PHP does not need to declare data type before being set. PHP automatically declare variable and the type of variable is determined at the run time depending on the context in which that variable is used. In PHP, a variable is identified by placing a $ in front of variable name. Variable name are case sensitive.
<?php $x = 10; $y=20; $z = $x + $y; // $z=$X+$Y does not produce result 30, because variable names are case sensitive. echo "The result is: ". $z; // Output will be 30 $name ='John'; $Name= "Mary"; echo "$name, $Name"; // The output will be John, Mary ?>
In the above example $ sign before x, y and z specifies that they are variables. The types of the variables are determined when they are being assigned. 10 determine the type of variable x, 20 determine the type of y and the result of x+y determines the type of z. We will discuss more about the types, assignment and predefined variables in the following tutorials.
- A variable name must begin with a letter or an underscore “-“.
- A variable name can only contain characters, numbers and underscore (a-z, A-Z, 0-9 and _).
- A variable name should not contain any special characters and space.