PHP Tutorial for Beginners

Filed Under: PHP

In the last post we saw how can we setup PHP for Mac OS X and this is my first PHP tutorial for beginners. This tutorial is for beginners to get them started with PHP.

What is PHP?

PHP is a server side scripting language and is used widely for making dynamic pages. PHP stands for “PHP Hypertext Processor” and its an open source scripting language. PHP scripts are run on server and its supported by major web servers like Apache, ISS and Nginx, making it a great tool to create beautiful dynamic web pages.

To learn PHP easily basic understanding of HTML and CSS is necessary. A PHP file contains HTML with php code that gets executed on the server and the result is a plain HTML page sent to the browser. PHP scripts can work with Files, Database and we can implement authentication and authorization for our web pages using PHP.

PHP Syntax

PHP script starts with <?php and ends with ?> and PHP file default extension is “.php”.


<?php
// PHP code
?>

Each statement ends with a semicolon and two basic statements used to output text is echo and print.

Comments in PHP

We can write comment in a line with two forward slashes or we can write comments in a block, syntax is shown below.


<html>
<body>

<?php
//PHP comment line

/*
PHP comment
block
*/
?>

</body>
</html>

PHP Variables


<?php
$x=3;
$y=4;
$z=$x+$y;
echo $z;
?>

The above PHP script will print 7.

  1. A variable starts with the $ sign, followed by the name of the variable like $abc
  2. Variable name must begin with a letter or the underscore character. Some valid names are $_abc, $a12 etc.
  3. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ). So $a.b is not a valid variable name.
  4. A variable name can’t contain spaces
  5. Variable names are case sensitive ($a and $A are two different variables)
  6. We don’t need to declare the type of variable, PHP automatically converts the variable to correct data type.

PHP Variables Scope

PHP Variables has four different scopes – local, global, static and parameter.

  1. Local Scope – If we declare a variable inside a function, then its scope is local and it can only be accessed with in the function. Local variables are deleted as soon as the function is executed.
    
    <?php
    $var=5; // global scope
    
    function test()
    {
    echo $var; // local scope
    }
    
    test();
    ?>
    

    The above script won’t print anything because echo statement is trying to lookup the local scope variable $var that is not initialized.

  2. Global Scope – If the variable is declared outside of a function then its scope is global and if we want to access a global variable inside a function, we need to use global keyword.
    
    <?php
    $a=5; // global scope
    $b=1; // global scope
    
    function test()
    {
    global $a,$b;
    $b=$a+$b;
    }
    
    echo $b; // output is 1
    test();
    echo $b; // output is 6
    ?>
    

    PHP also stores all the global variables in an array $GLOBALS and using variable name, it can be accessed using this array directly anywhere in the PHP script.

    Above PHP script can also be written in following way;

    
    <?php
    $a=5; // global scope
    $b=1; // global scope
    
    function test()
    {
    $GLOBALS['b']=$GLOBALS['a']+$GLOBALS['b'];
    }
    
    echo $GLOBALS['b']; // output is 1
    test();
    echo $GLOBALS['b']; // output is 6
    ?>
    
  3. Static Scope – If you want a local variable not to be deleted after the function is executed, you can declare it as static. The static variable is however local to the function only and can’t be referenced in other functions. We can use static variable to count the number of times a function is executed.
    
    <?php
    
    function test()
    {
    static $x=0;
    echo $x;
    $x++;
    }
    
    function test1()
    {
    echo $x; //this won't print anything because $x is static but local to test()
    $x++;
    }
    
    test();
    test1();
    test();
    
    ?>
    

    The above script will print “01”.

  4. Parameter Scope – A parameter is a local variable passed to the function by the code calling it. parameters are also called arguments.
    
    <?php
    $y=10;
    function test($x) //$x is a parameter and have parameter scope.
    {
    echo $x;
    $x=$x+1;
    }
    
    test($y); // passing parameter
    test($y); // value of $y still remains 10
    test($y);
    ?>
    

PHP String

String is a stream of characters and in PHP we can use single or double quote with value to create a String and we can assign it to any variable.

We can use period (.) to concat two Strings. Some other useful String functions are strlen() that returns the length of the String and strpos() that can be used to search any character or text in the String. Lets see these in below example.


<?php
$str1 = "Pankaj";
$str2 = 'Kumar';

echo $str1 . " " . $str2; // prints "Pankaj Kumar"

echo "<br>"; // for printing in new line, echoing HTML new line code

echo strlen($str1);   // prints 6
echo strlen($str2);   // prints 5

echo strpos($str1,"a"); //prints 1, index starts with 0
echo strpos($str2,'k'); //prints nothing, PHP is case sensitive

?> 

Below image shows the output HTML produced when I execute it using PHP command.

php tutorial for beginners

I think it’s enough to end this PHP tutorial for beginners, in next post I will focus on PHP Operators, conditional statements, PHP Arrays and for/while loops.

Reference: Official Doc

Comments

  1. christoper stalin says:

    many thanks for this blog, because it is very useful for beginners.

  2. benlewis says:

    great nic tutorial for beginners thanks for this wonderful post

    Retrieving data. Wait a few seconds and try to cut or copy again.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages