sap4help@gmail.com +91 8956877466
This website is always under updation with new contents
PHP Tutorials
Introduction
What is PHP
What is echo
What is print
Variable
Variable Scope
Operators
Basic Operators
Assignment Operators
Comparison Operators
Increment Decrement Operator
Ternary operator
PHP Comment
Datatype
Control Structure
If ElseIf Else
Switch Statement
Looping
While loop
Do While loop
PHP Arrays
Index Array
Associative Array
Printing Arrays
Processing Arrays
Multidimensional Arrays
PHP String
String Function
Math's Function
PHP Form
GET Method
Post Method
PHP Include
PHP Require
Require vs Include
State management
PHP Session
Cookies
File Handling
Append Files
Reading Files
Writing Files
Deleting Files
PHP (Hypertext Pre Processor)
Introduction

If you’re reading this book, you probably already know what PHP is—one of the world’s most popular programming languages for Web application development. Widely available and backed by the support of a vociferous and enthusiastic user community, the language was in use on more than 20 million Web sites at the end of 2006…and that number is only expected to grow..!

Personally, I’ve always believed the reason for PHP’s popularity to be fairly simple. It has the unique distinction of being the only open-source server-side scripting language that’s both easy to learn and extremely powerful to use. Unlike most modern server-side languages, PHP uses clear, simple syntax and delights in non-obfuscated code; this makes it easy to read and understand, and encourages rapid application development. And then of course, there’s cost and availability.

PHP is available free of charge on the Internet, for a variety of platforms and architectures, including UNIX, Microsoft Windows, and Mac OS, as well as for most Web servers.


What is PHP?

PHP (Hyper text Pre Processor) is a scripting language commonly used for web applications. PHP can be easily embedded in HTML. PHP generally runs on a web server. It is available for free and can be used across a variety of servers, operating systems and platforms.


What is echo statement

The echo statement can output one or more string. The echo statement can display anything that can be display to the browser, such as string, variable value, number, the result of expression etc.

Echo is a language construct not actually a function. You can used it without parentheses e.g echo or echo(). If you want to pass more than one parameter to echo, the parameter must not be enclosed within parentheses.


What is print

The print statement can also use for displaying the output to the browser. The print is also a language construct not a real function. So can also use it without parentheses like: print or print().

The echo and print both statement are work exactly the same way except that the print statement can only output one string, and always return 1. That’s way the echo statement considered marginally faster than the print statement since is doesn’t return any value.


Variables

Variable is name given to the storage area and its value change during programming execution is known as variable. The variable are use to store data. like number of arrays.

PHP variables begin with a dollar sign ($) as shown below.

Syntax
$var_name = "Value";

Variable TypeExplanation
booleanTrue or false
arrayTList of items
objectInstall of a class

Variable Names (Identifiers )
  • consist of letters, digits, underscores and dollar signs
  • cannot begin with a digit
  • are case sensitive
  • Code Sample: HelloVariables.php
    <?php
    $Greeting = 'Hello World!';
    ?>
    <html>
    <head>
    <title><?php echo $Greeting; ?></title>
    </head>
    <body>
    <?php
    echo $Greeting;
    ?>
    </body>
    </html>
    O/P
    Hello World!

    Rules for PHP variables
    1. A variable name start with a letter or the underscore character
    2. A variable name can not start with number
    3. A variable start with the $ sign, followed by the name of the variable
    4. Variable name are case-sensitive ($age and $AGE are two different variable)
    5. A variable name can only contain alpha-numeric characters and underscore (A-z, 0-9, and _)

    Variable Scope

    A variable's scope determines the locations from which the variable can be accessed.

    PHP variables are either super global, global, or local.
    Super global

    Superglobal variables are predefined arrays, including $_POST and $_GET. They are accessible from anywhere on the page.

    The complete list of super global is shown below.
  • $_GET - variables passed into a page on the query string.
  • $_POST - variables passed into a page through a form using the post method.
  • $_SERVER - server environment variables (e.g, $_SERVER['HTTP_REFERER'] returns the URL of the referring page).
  • $_COOKIE - cookie variables.
  • $_FILES - variables containing information about uploaded files.
  • $_ENV - PHP environment variables (e.g, $_ENV['HTTP_HOST'] returns the name of the host server.
  • $_REQUEST - variables passed into a page through forms, the query string and cookies.
  • $_SESSION - session variables.

  • Global

    Global variables are visible throughout the script in which they are declared. However, they are not visible within functions in the script unless they are re-declared within the function as global variables


    Function

    Variables in the function scope are called local variables. Local variables are local to the function in which they are declared.


    Constants

    Constants are like variables except that, once assigned a value, they cannot be changed. Constants are created using the define() function and by convention (but not by rule) are in all uppercase letters. Constants can be accessed from anywhere on the page.

    A constant Is a identifier for a simple value. As the name suggest, that value cannot change during execution of the script.

    Syntax
    define('CONST_NAME',VALUE);

    Operators

    When comparing or processing variables and other values you use operators. Without them, PHP would be more of a word jumble instead of a language. In some unique cases, operators slightly alter the relationship between two variables or their function within PHP. Without further adieu, here they are.


    Basic Operators
    Add ( + )$a = 1; $a = $a + 5; // $a is equal to 6
    Subtract ( - )$s = 10; $s = $s - 5; // $s is equal to 5
    Multiply ( * )$m = 2; $m = $m * 10; // $m is equal to 20
    Divide ( / )$d = 20; $d = $d / 5; // $d is equal to 4
    Modulus ( % )Provides the remainder after division: $u = 5; $u = $u % 2; // $u is equal to 1

    Assignment Operators
    Add ( += )$a = 1; $a += 5; // $a is equal to 6
    Subtract ( -= )$a = 1; $a += 5; // $a is equal to 6
    Multiply ( *= )$m = 2; $m *= 10; // $m is equal to 20
    Divide ( /= )$d = 20; $d /= 5; // $d is equal to 4
    Modulus ( %= )Provides the remainder after division: $u = 5; $u %= 2; // $u is equal to 1
    Concatenate ( .= )Join onto the end of a string: $c = 5; $c .= 2; // $c is now a string, '52'

    Comparison Operators
    Greater Than ( > )2 > 1
    Less Than ( < )1 < 2
    Greater Than or Equal To ( >= )2 >= 2 3 >= 2
    Less Than or Equal To ( <= )2 <= 2 2 <= 3

    Increment decrement operator

    Increment ( $integer++; )

    Decrement ( $integer--; )

    Example:
    $a = 1;
    $a = $a + 1; // $a is now equal to 2
    $a++; // $a is now equal to 3
    $a--; // $a is now equal to 2 again, same as $a = $a – 1;

    Ternary operator

    The Ternary Operator is a short-hand form for evaluating what to do when an expression is evaluated as either TRUE or FALSE. The conditional returns either the TRUE or FALSE output. Basic format is as follows:

    (expr) ?ValueIfTrue :ValueIfFalse ;
    Examples:
    $boolean = TRUE;
    $result = ($boolean) ? 'Is True' : 'Is False';
    echo $result;
    Is True
    // $result is not yet set
    $result = (isset($result)) ? $result+1 : 10;
    echo " \$result = $result.";
    $result = (isset($result)) ? $result+1 : 10;
    echo " \$result = $result.";
    $result = 10. $result = 11.
    PHP Comment

    When it comes to altering the case of a string, PHP makes it easy with four built-in functions. Two of them are illustrated previously: the strtoupper() function uppercases all the characters in a string, while the strtolower() function lowercases all the characters in a string.

    For more precise control, consider the ucfirst() function, which capitalizes the first character of a string (good for sentences), and the ucwords() function, which capitalizes the first character of every word in the string (good for titles).

    Here’s an example:
    <?php
    // define string
    $rhyme = "and all the king's men couldn't put him together again";
    // uppercase first character of string
    // result: "And all the king's men couldn't put him together again"
    $ucfstr = ucfirst($rhyme);
    echo $ucfstr;
    // uppercase first character of every word of string
    // result: "And All The King's Men Couldn't Put Him Together Again"
    $ucwstr = ucwords($rhyme);
    echo $ucwstr;
    ?>

    For accessing computer programs go to TECHNOLOGY