PHP Coding:

  1. PHP will fall in between the HTML body tags
  2. PHP scripts always start with <?php and end with ?>
    1. Each line of code of PHP ends with a ;
  3. To print, you can use "echo" or "print".

Sample:

<?php echo "hello"; ?>

  1. Variables in PHP start with a $
    1. <?php
    2. $txt = "hello";
    3. echo $txt;
    4. ?>
  2. You can connect multiple variables with a .
    1. $txt1 = "hello";
    2. $txt2 = "class";
    3. echo $txt1 . $txt2;
  3. You can make comments in your coding to help you or others understand what you've done. You do this with //
    1. //this is a comment
    2. /*
    1. */
  4. PHP functions:
    1. phpinfo () - this allows you to see different types of information about a computer system as well as the way PHP is set up.
      1. Try the following between the ()
        1. INFO_GENERAL
        2. INFO_CREDITS
        3. INFO_CONFIGURATION
        4. INFO_MODULES
        5. INFO_ENVIRONMENT
        6. INFO_VARIABLES
        7. INFO_LICENSE
        8. INFO_ALL
        9. This is by no means a full list of functions in PHP
    2. PHP Server Variables
      1. The servers hold a variety of information that could be of use. The variable $_SERVER is a reserved variable that contains all server information.
        1. "Referer: " . $_SERVER["HTTP_REFERER"] . "<br />";
        2. "Browser: " . $_SERVER["HTTP_USER_AGENT"] . "br />";
        3. "User's IP address: " . $_SERVER["REMOTE_ADDR"];
        4. Each of the above will, if used with the "echo" statement give you the user's URL from which they came, their Browser and their IP address.
    3. Header()
      1. sends raw HTTP headers through HTTP protocol ... for example you can redirect someone to another page.
        1. header ("Location: http://www.apple.com/");
    4. fopen()
      1. Allows php to open a specifiet file.
        1. $f=fopen("welcome.txt", "r");
          • we're opening the file "welcome.txt" as a read only file and assigning it to the variable $f.
        • you can also check to see if it can or can't open the file and return a comment.
          • if (!($f=fopen("welcome.txt", r)))
          • exit (unable to open file!);
  5. Conditional Statements:
    1. There are two conditional statements:
      1. if (...else) which allows you to have code executed if a given condition is true and something else executed if it is not true
        • $d=date("D");
        • if ($d=="Fri")
        • echo "Have a nice weekend!";
        • else
        • echo "Have a nice day!";
        • If more then one condition is to be executed, those lines should be in {}
          • if ($x-=10)
          • {
          • echo "Hello<br />";
          • echo "Good morning<br />";
          • }
      2. switch statement which allows you to select multiple lines of code to execute.
        • //first we have a variable that is evaluated once.
        • switch ($x)
        • //the value is then compared with each case, if it matches, the code is executed.
        • {
        • case 1:
          • echo "Number 1";
          • break;
        • case 2:
          • echo "Number 2";
          • break;
        • case 3:
          • echo "Number 3";
          • break;
        • default:
          • eco "No number between 1 and 3";
        • }