Site menu
Search
Map

View Larger Map
Calendar
«  September 2017  »
SuMoTuWeThFrSa
     12
3456789
10111213141516
17181920212223
24252627282930
Site friends
  • Create a free website
  • Statistics

    Total online: 1
    Guests: 1
    Users: 0
    Ads
     First Hour
    PHP - First Hour

    ***** First Hour *****

    From this hour you will be able to learn about -

    What is PHP?
    What is the advantages of PHP?
    What is a PHP file?
    Which software is needed to get output of PHP file?
    What is the basic syntax of PHP script?
    How to save and run the file?
    A Simple PHP Script
    What is Constants?
    A simple example of using constants
    What is variables?
    Variable naming rules
    A simple example of using variable
    A simple example of using URL variable
    A simple example of using Special Character in URL variable

     
     
    What is PHP? Top

    PHP: PHP is a cross-platform server-side scripting language. It is used to develop web database application. At first, it was developed for make personal home page by Rasmus Leodrof in 1994. For this reason its name was Personal Home Page. Now it is a strong scripting language and it is called Hypertext Preprocessor.

    What is the advantages of PHP? Top
    • PHP is simple and easy to learn, and incredibly powerful
    • PHP runs on different platforms, like – Windows, Linux, UNIX, Novell NetWare, etc.
    • PHP is compatible with almost all web servers, like – Apache, IIS
    • PHP is used on many websites across the world
    • PHP is a server-side scripting language, like – ASP
    • PHP scripts are executed on the server
    • PHP supports many databases, like – MySQL, Oracle, Informix, Sybase, Solid, PostgreSQL, Generic ODBC, etc.
    • PHP is open source and free to download from the official PHP resource
    What is a PHP file? Top
    • PHP files can contain text, HTML tags and scripts
    • PHP files are returned to the browser as plain HTML
    • PHP files have a file extension of .php, .php3, .php4, .php5, .php6, or .phtml
    Which software is needed to get output of PHP file? Top
    • PHP software
    • A web server, like – Apache, IIS, etc.
    • A browser, like - Internet Explorer, Mozilla Firefox, etc.
    • A database software, like - MySQL, Oracle, Access, etc. (to store data)
    What is the basic Syntax of PHP? Top

    A PHP scripting block always starts with <?php or <? and ends with ?>, but for maximum compatibility, we recommend that you use the standard form (<?php) rather than the shorthand form (<?).
    A PHP file normally contains HTML tags just like an HTML file, and some PHP scripting code.  Follow the below example which sends the text "Hello World" to the browser –

    <html>
    <body>
    <?php
    echo "Hello World";
    ?>
    </body>
    </html>

    Note: Here –

    • <html> and <body> is starting tag for HTML
    • <?php is starting tag for PHP
    • 'echo' is a built-in function which is used to publish "Hello World" text or you can use 'print' built-in function for publishing text, like – print "Hello World".
    • echo "Hello World" is a statement and it is finished by semicolon (;). So after finish a statement you must have to use semicolon (;).
    •   ?> is ends tag for PHP
    • </body> and </html> is ends tag for HTML.
    How to save and run the file? Top
    • Open NotePad and type above code
    • Save the file into htdocs folder with .php extension, as – hello.php
    • Run apache or other web server
    • Open browser, as – Internet Explorer or other
    • Type http://localhost/hello.php and press enter
    A simple PHP script Top

    <html>
    <head>
    <title>A Simle PHP script</title>
    </head>
    <body>
    <?php
    $today = date("d/m/y"); //day/month/year
    $now = date("h:i:s: a"); //hour:min:sec AM/PM
    $output1 = "The time is $now";
    $output2 = "The date is $today";
    echo ("</p>".$output1."<br>\n".$output2."</p>");
    ?>
    <body>
    </html>

    Note: The code will produce an HTML document that states, for example:

    The time is 09:02:11: pm
    The date is 27/01/11

    Overview of Constants with example Top

    A constants is placeholder for a value that you reference within your code that formally defined before using it. When naming constants, remember they must begin with a letter or an underscoure, and cannot begin with a number. Names are also case-sensitive, though typically they are named using all capital letters so you can easily identify them within your code.

    You define a value assigned to a constant with the PHP function define(). Once you've defined a constant, it can't be changed or undefined.

    Example -

    <html>
    <head>
    <title>Using Constants</title>
    </head>
    <body>
    <?php
    define ('FAVMOVIE', 'The Life of Brain');
    echo 'My favorite movie is ';
    echo FAVMOVIE;
    ?>
    </body>
    </html>

    Note: Open your text editor > Type the above code > Save as favmovie.php > Open it in your browser.

    You should see the text as - My favorite movie is The Life of Brain

    Overview of Variables Top

    A variable is a means of storing a value, such as text string "Hello World!" or the integer value 4. A variable can then be throughout your code, insted of having to type out the actual value over and over again.

    In PHP you define a variable with the following form:

    • $variable_name = value;

    <?php
    <$hello = "Hello World!";
    <$a_number = 4;
    <$anotherNumber = 8;
    ?>

    PHP is not strongly typed - a variable's type is determined by the type of value that you place in it, and is changed depending on the context that you use it in. E.g.:

    $integer = 279; //An integer variable
    $string = "1"; //An string variable
    $concat = $string.$integer; //=280
    $add = $integer+$string; //= 280
    $insert = "$integer $string"; //= "279 1"

    As you can see, you can also use variable names inside of strings to insert the value of the variable at the position in the string.

    PHP variable naming conventions Top

    There are a few rules that you need to follow when choosing a name for your PHP variables.

    • PHP variables must start with a letter or underscore "_".
    • PHP variables may only be comprised of alpha-numaric characters and underscoures. a-z, A-Z, 0-9, or _.
    • Variables with more than one word should be separated with underscoures. $my_variable
    • Variables with more than one word can also be distinguished with capitalization. $myVariable

    Example -

    <html>
    <head>
    <title>Using Variable</title>
    </head>
    <body>
    <?php
    define ('FAVMOVIE', 'The Life of Brain');
    echo 'My favorite movie is ';
    echo FAVMOVIE;
    echo '<br/>';
    $movierate = 5;
    echo 'My movie rating for this movie is: ';
    echo $movierate;
    ?>
    </body>
    </html>

    Note: Open your text editor > Type the above code > Save as movierate.php > Open it in your browser.

    You should see the text as -

    My favorite movie is The Life of Brain
    My movie rating for this movie is: 5

    Example of using URL variable Top

    Step - 1. Open text editor, and type the following program, and save as urlvariable1.php:

    <html>
    <head>
    <title>Using URL Variable - <?php echo $_GET ['favmovie']; ?></title>
    </head>
    <body>
    <?php
    echo 'My favorite movie is ';
    echo $_GET['favmovie'];
    echo '<br/>';
    $movierate = 5;
    echo 'My movie rating for this movie is: ';
    echo $movierate;
    ?>
    </body>
    </html>

    Step - 2. Start a new document in your text editor, and type the following program, and save as movie1.php

    <html>
    <head>
    <title>Find my Favorit Movie!</title>
    </head>
    <body>
    <?php
    print '<a href="urlvariable1.php?favmovie=Stripes">';
    print 'Click here to see information about my favorite movie!';
    print '</a>';
    ?>
    </body>
    </html>

    Step - 3. Open the file (movie1.php) in your browser, and click the link, and you should see the text as -

    My favorite movie is Stripes
    My movie rating for this movie is: 5

    Example of using Special Character in URL Variable Top

    Step - 1. Open text editor, and type the following program, and save as scurlvariable1.php:

    <html>
    <head>
    <title>Using URL Variable - <?php echo $_GET ['favmovie']; ?></title>
    </head>
    <body>
    <?php
    echo 'My favorite movie is ';
    echo $_GET['favmovie'];
    echo '<br/>';
    $movierate = 5;
    echo 'My movie rating for this movie is: ';
    echo $movierate;
    ?>
    </body>
    </html>

    Step - 2. Start a new document in your text editor, and type the following program, and save as movie2.php

    <html>
    <head>
    <title>Useing Special Character in URLs Variable</title>
    </head>
    <body>
    <?php
    $myfavmovie = urlencode('Dil to Pagal Ha!');

    print "<a href=\"scurlvariable1.php?favmovie=$myfavmovie\">";
    print 'Click here to see information about my favorite movie!';
    print '</a>';
    ?>
    </body>
    </html>

    Step - 3. Open the file (movie2.php) in your browser, and click the link, and you should see the text as -

    My favorite movie is Dil to Pagal Ha!
    My movie rating for this movie is: 5

    Copyright MyCorp © 2017