banner I am Dev. A Freelance Web Developer from India. I have been working on for 2 years on PHP/MySQL, AJAX and related coding.

And personally I almost like everything that is OpenSource. I usally experiement with different opensource projects like RubyOnRails, OpenSocial, Facebook API and many more. More...
Oct
14th

PHP Crash Course For Beginner’s - Part 1

Author: Dev | Files under PHP

PHP Crash Course Part 1

I am writing a crash course on Learning PHP For Beginner’s. The cource will be divided into several parts. Here is the Part I of the course.

What should I know before reading this tutorial?

Only thing is you are expected to be familer with the basic HTML markup. And experience in any kind of programming is a plus.

What is PHP?

  • PHP is a server side scripting language, written specifically for the Web. And it rules the web with usage over 20 million domains as April 2007. Check Latest PHP Statistics fore more.
  • PHP was developed in 1994 by Rasmus Lerdorf. He wrote it as a way to track visitors to his online CV.
  • PHP originally stands for Personal Home Page
  • PHP now stands for PHP Hypertext Preprocessor (a recursive acronym) after Zeev Suraski and Andi Gutmans rewrote the main parser in mid-1997,
  • Current version of PHP is 5.2.6
  • Current version of Historical PHP4 is 4.4.9
  • Version 6 is available for early adopters
  • Home page = http://www.php.net
  • PHP strengths include: High performance, Interfacing with Different DBMS (uses ODBC), Libraries, Low Cost, Portability, open source, and thousands of functions.
  • Web server must have PHP installed.
  • PHP scripts are interpreted and executed on the server.
  • Output from a PHP script looks like plain old HTML. The client cannot see your PHP code, only its output.
  • PHP can be used in many ways check offical article on What can PHP do?

WHAT IS THE PHP ‘ENGINE’?

In a nutshell: The PHP engine is just a program that knows how to read and process PHP code.

Let me explain with an example.

<html>

<head>

<title>Example</title>

</head>

<body>

<?php

echo “Hello, world!”;

?>

</body>

</html>

In PHP (like in all programming languages,) there are special keywords that tell the PHP engine to do something.

In the above example we are requesting the PHP engine to display the text “Hello Worl!”, here we use ‘echo’, a special keyword/command that tells the PHP engine to print something to the HTML page.

Now you know PHP pages are just like HTML pages, except that they have special PHP code in places surrounded by the special PHP start and end tags. Generally, all PHP pages should have the file name extension ‘.php’ rather than ‘.html’.

Consider if you have an HTML page called: ‘photos.html’ and you wanted to add some PHP code into the page to grab a list of photos from a database, you would first have to change the page name to: ‘photos.php’ and then insert the PHP code into your page.

You need to rename any html pages that have PHP code in them to ‘.php’, so that the PHP engine knows that there is PHP code in there.

Finally, you cannot run PHP pages unless your server has PHP installed. This is very likely since PHP is free for everyone, and runs on both Windows and non-Windows servers like Linux.

Installing & Starting PHP Engine

Generally, PHP Engine alone cannot do much of the things of a typical website needs.

We need two more servers along with PHP Engine to run a successful website.

1) Apache - a HTTP webserver for handling requests & responses. Here, PHP requests and HTML responses are handled by Apache Server. that uses Structured Query Language (SQL), the most popular language for adding, accessing, and processing d

2) MySQL - a database serverata in a database. Here, we use MySQL server to access databases via PHP to easy insert, update and delete data.

So, now we need Apache, MySQL, PHP Engine installed on the system to run PHP pages.
See the article on Installing Apache, MySQL and PHP Engine for installing WAMP under Windows.

PHP Tags –

  • Short Style <? echo ”<p>Short Style</p>”; ?>
  • XML Style <?php print(“<p>XML Style</p>”); ?>
  • Script Style <script language=’php’>printf(“<p>Script Style</p>”);</script>
  • ASP Style <% echo “<p>ASP Style</p>”; %>
  • An introductory example:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    
    "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
        <head>
    
            <title>Example</title>
    
        </head>
    
        <body>
    
            <?php
    
    				echo "Hi, I'm a PHP script!";
    
            ?>
    
        </body>
    
    </html>

    OutPut ( as HTML):

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    
    "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    
        <head>
    
            <title>Example</title>
    
        </head>
    
        <body>
    
            Hi, I'm a PHP script!
    
        </body>
    
    </html>

PHP Comments –

  • C Style Example: /* this is a multi line comment */
  • C++ Style Example: // this is a single line comment
  • PERL Style Example: # this is a single line comment
    Example:

    <?php

    # This is a comment

    //This is a comment

    /*

    This a comment

    in multiple lines

    */

    echo(”Comment Example”);

    ?>

Most PHP code statements must end with a ; (semi-colon)

  • Conditionals and loops are an exception to the rule.
    Example:

    <?php

    for( $i=0; $i<10; $i++ ){

    echo($i);

    }

    ?>

    In the above example you can see the for loop does not end with a semi-colon, where as the statement echo ends with semi-colon;

Variables:

  • All variables in PHP begin with $ (dollar sign)
  • All variables are created the first time they are assigned a value.
  • PHP does not require you to declare variables before using them.
  • PHP variables are multi-type (may contain different data types at different times)
  • Variables from a form are the name of the attribute from the submitting form.
  • Can have any length, may consist of letters, numbers, underscores, and $
  • Cannot start with a digit.
  • Are case sensitive.
  • Can have the same name as built-in functions, but avoid doing so.
    Example:

    <?php

    var $a = 1; // variable with declaration

    $b = 1; // Variable without declaration

    $_c = “test123″; // Correct Variable Usage

    $1b = “1″; // Error : Incorrect Variable Usage , cannot start with a digit

    ?>

PHP’s data types:

  • Integer (whole numbers).
  • Float (real numbers).
  • String (text enclosed in single or double quotes).
  • Boolean (true or false).
  • Object (instance of a class).
  • Array (group of values, usually of the same type).
  • Resource (external data source i.e. database record).
  • Null (null – for undeclared, uninitialized variables or those set to NULL).

    Example:

    <?php

    $name_array = array(”john”,”micheal”,”casper”); // $name_arrat variable is of data type Array and initialised with 3 items.

    $inspect = true; // $inspect is a Boolean variable usage true or false (case-insensitive)

    $fruit = “apple”; // fruit is of type String

    $price = “12.34″; // $price is of type Float

    ?>

Integers:

  • Integers can be specified in decimal notation, optionally preceded by a sign (- or +).
  • octal (8-based) notation is preceded with a 0, optionally preceded by a sign (- or +).
  • hexadecimal notation precedes the number with 0x.
  • Examples:
    $a = 1234; # decimal number

    $a = -123; # a negative number

    $a = 0123; # octal number (equivalent to 83 decimal)

    $a = 0×1A; # hexadecimal number (equivalent to 26 decimal)

Floats: (a.k.a. doubles, real numbers):

  • Floating point numbers can be specified using any of the following syntaxes:
  • Examples:
    $a = 1.234;

    $a = 1.2e3;

    $a = 7E-10;

  • The size of a float is platform-dependent, although a maximum of ~1.8e308 with aprecision of roughly 14 decimal digits is a common value (that’s 64 bit IEEE format).
Like this post? Want to receive more like these!

Then, enter your email address here:

Delivered by FeedBurner


Do you like this article? then

Post a Comment