Using PHP

http://php.net/manual/en/
phpinfo

PHP is a server-side language. That is, PHP files are processed through a PHP Web server.
PHP code > processed through PHP parser (or engine) > returned to client > interpreted by browser > viewed by user
PHP files may include some or all of the following elements:
  1. Introduction: overview, comments, statements, output
  2. variables: reserved memory locations that hold (changing) values
  3. arrays: reserved memory locations that hold multiple (changing) values
  4. conditional statements: or decision-making statements (if...else)
  5. loops: repetitive tasks
  6. functions: predefined tasks
    Example: isset() vs empty()
  7. strings
  8. forms
  9. includes
  10. query strings
  11. sessions
  12. debugging

Introduction

All PHP code must be surrounded by the following PHP tags:
<?php
... PHP code ...
?>
All PHP statements *must* end with semicolon (;):
<?php
do something;
?>

Comments

Use comments to internally document code that cannot be seen by the user. (Also, good for debugging!)
Three Methods:
  1. Single-line comment: pound sign (#), Unix shell-style or Perl style
  2. Single-line comment: two forward slashes (//), C-Style
  3. Multi-line comment: (/* ... */), C-Style
Note: C-style comments end at the first */ encountered.
Make sure you don't nest C-style comments.
/*
    echo 'Hello World'; /* This comment will cause error. */
 */

Output

  • There are various ways to output values.
  • Both print() and echo() are not actual functions (but, rather language constructs) so, parentheses are not required.
  • Also, sprintf() returns a string while printf() outputs a string (to standard output):
  • <?php
    echo("Text here");
    //is equivalent to:
    echo "Text here";
    
    print("Text here");
    //is equivalent to:
    print "Text here";
    
    printf("Text here"); 
    //is equivalent to:
    echo sprintf("Text here"); 
    ?>
    
    Text here
    Text here
    Text here
    Text here
    Text here
    Text here

    Variables

    Naming variables: Assigning Values to Variables:
    <?php
    $myVar = 10;
    echo $myVar;
    
    $yourVar = "Hello World";
    echo $yourVar;
    
    /*
    Variables that have no value assigned are of type null. 
    The size of null is 0 bytes. 
    Assigning null to a variable deletes the contents of that variable from memory.
    
    However, unset() function removes the variable, as if it were never
    declared (see error below, with error_reporting turned on.)
    */
    
    $yourVar = null; //removes contents of variable
    unset($yourVar); //removes variable from memory
    
    //addition returns 10
    echo 5 + "5";
    
    //concatenation (dot) operator returns 55
    echo 5 . 5;
    ?>
    
    10
    Hello World

    10
    55

    var_dump():
  • var_dump() is a very useful PHP function when debugging, and/or tracking variables.
  • In addition, var_dump() function displays variable details, including contents.
  • It will accept any expression. After processing the result will be displayed.
  • In addition, multiple arguments may be passed to the function by separating each with a comma (,).
  • <?php
    $myVar = 15;
    echo $myVar;
    
    $yourVar = "Hello Universe";
    echo $yourVar;
    
    var_dump($myVar, $yourVar);
    
    $myVar = null; //removes contents of variable
    unset($yourVar); //removes variable from memory
    
    var_dump($myVar, $yourVar); //will generate error, with error_reporting turned on.
    ?>
    
    15
    Hello Universe
    int(15)
    string(14) "Hello Universe"
    
    Warning: Undefined variable $yourVar in D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php on line 267
    NULL NULL
    Converting Special Characters to HTML Entities:
    Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. For example:
    Some predefined characters: The following two functions return a string with these conversions made:
    Using htmlentities() or htmlspecialchars():
    The htmlspecialchars() and htmlentities() functions convert predefined characters to HTML entities. Depending upon the charset used, these conversions are necessary in order to be displayed w/in a browser, or used w/in XML.

    Note: The conversions *must* be viewed using the "View Source" command.

    htmlspecialchars(): Convert special characters to HTML entities.
    Example:
    $specialchars = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
    echo $specialchars; 
    
    // view source returns: <a href='test'>Test</a>
    
    Result:
    <a href='test'>Test</a>

    no htmlspecialchars() -- that is, no conversion of special characters
    $no_specialchars = "<a href='test'>Test</a>";
    echo $no_specialchars; 
    
    Result:
    Test

    htmlentities(): Convert *ALL* applicable characters to HTML entities.
    Identical to htmlspecialchars() in all ways, except with htmlentities(), *ALL* characters which have HTML character entity equivalents are translated into these entities.
    Example:
    $str = "A 'quotation' may be <b>bold</b>.";
    echo htmlentities($str);
    
    // Or, to handle code as XHTML, and encode in utf-8 charset
    echo htmlentities($str, ENT_QUOTES | 'ENT_XHTML', "UTF-8");
    
    Result:
    A 'quotation' may be <b>bold</b>.

    Fatal error: Uncaught TypeError: Unsupported operand types: int | string in D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php:354 Stack trace: #0 {main} thrown in D:\InetPub\vhosts\mjowett-1638.package\qcitr.com\wwwroot\demos\index.php on line 354