Twitter For Webanddesigners
RSS For Webanddesigners

Introduction to PHP – Part 1

Introduction:

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.

Installing PHP:

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.

Basic Syntax:

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.

Example# 1:


<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?.

Example# 2:

<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>


Defining Variables:

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.


Example# 3:

<?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.

Naming rules:

  • 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.



Related Posts


bookmark bookmark bookmark bookmark Subscribe to RSS


23 Comments »

  1. wajer says:

    Hi really nice one, really looking forward for next parts
    cheers

  2. WAD says:

    You won’t have to wait long.

  3. bhanu says:

    hey

    more informative plz forward remaining parts

  4. bab says:

    I found a great article about PHP for newbies

  5. Bab says:

    I found a good article about PHP for newbies here
    visit
    http://cbabeesh.blogspot.com/

  6. Mia says:

    I will definitely be returning here tomorrow ( after Ive slept a bit :D ) and I will def be studying all of these tuts ( so I can gain some insight & know what the heck Im doing – before I end up blowing up the web :D ) . * Please list your 1-800 number for newbies like me :) ya think u can ? :P – * I shall return ! Great tuts & rockin articles !

    M. 

  7. Mia says:

    ** P.S: FIX ur Retweet Button – it shouldnt say RT@tweetmeme – it should be RT@ USERNAME – Im shocked ! :P * anyway , fix it :) Catch ya in cyberspace !

  8. WAD says:

    @Mia Fixed

  9. Mia says:

    Good hackey :D – but I just noticed Bab’s comment ^ – that is too funny LOL : why would someone be writing PHP scripts and be on Blogger ? If they possess such expertise at PHP why not be on WP & back up what theyre writing about ?? Im sorry I cant stop laughing at that I find it so funny :D

  10. David says:

    Excelente! Muchas Gracias.

Trackbacks

  1. » Introduction to PHP – Part 1 | Devmarks
  2. Introduction to PHP – Part 1
  3. Introduction to PHP – Part 1 | Programming Blog
  4. Introduction to PHP – Part 1 | meshdairy
  5. Introduction to PHP – Part 1 | Master Design
  6. 80+ Awesome Community Links for Designers and Developers | tripwire magazine
  7. 80+ Awesome Community Links for Designers and Developers | Programming Blog
  8. 80+ Links for Designers and Developers – รวมมาแล้วบทความสำหรับ Webmaster | ไอทีสนุกดอทคอม
  9. 80+ Awesome Community Links for Designers and Developers | Master Design
  10. Favorites From The Feeds #1: All The Stuffs You Need | AEXT.NET
  11. Favorites From The Feeds #1: All The Stuffs You Need | Master Design
  12. Top WordPress Code Sites | Websites for Small Biz
  13. PHP, the other other white meat « Devin Lynch Design – Blog

RSS feed for comments on this post. TrackBack URL

Leave a comment

Spam Protection by WP-SpamFree