Skip to main contentdfsdf

    • The following example will output "Have a nice weekend!" if the current day  is Friday, and "Have a nice Sunday!" if the current day is Sunday. Otherwise it will output "Have a nice day!":

        
       
      <html> <body>
       
      <?php $d=date("D"); if ($d=="Fri")   echo "Have a nice weekend!";  elseif ($d=="Sun")   echo "Have a nice Sunday!";  else   echo "Have a nice day!";  ?>
       
      </body> </html>
    • The concatenation operator (.)  is used to put two string values together.

       

      To concatenate two variables together, use the dot (.) operator:

       
       
      <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?>
    • The concatenation operator (.)  is used to put two string values together.

       

      To concatenate two variables together, use the dot (.) operator:

       
       
      <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?>
       
       

      The output of the code above will be:

        
       
      Hello World 1234

    2 more annotations...

    • All variables in PHP start with a $ sign symbol.

       

      The correct way of setting a variable in PHP:

       
       
      $var_name = value;
    • Let's try creating a variable with a string, and a variable with a number:

       
       
      <?php $txt = "Hello World!"; $number = 16; ?>

    1 more annotation...

    • Basic PHP Syntax

       

      A PHP scripting block always starts with <?php and ends with ?>.  A PHP scripting block can be placed anywhere in the document.

    • The file must have the .php extension. If the file has a .html  extension, the PHP code will not be executed.
      • In PHP we have the following looping statements:

         
           
        • while - loops through a block of code if and as long as a specified condition is true
        •  
        • do...while - loops through a block of code once, and then repeats the loop   as long as a special condition is true
        •  
        • for - loops through a block of code a specified number of times
        •  
        • foreach - loops through a block of code for each element in an   array
      • Syntax

            
         
        for (init; cond; incr) {   code to be executed; }
         
         

        Parameters:

         
           
        • init: Is mostly used to set a counter, but can be any code to be   executed once at the beginning of the loop statement.
        •  
        • cond: Is evaluated at beginning of each loop iteration. If the   condition evaluates to TRUE, the loop continues and the code executes. If it   evaluates to FALSE, the execution of the loop ends.
        •  
        • incr: Is mostly used to increment a counter, but can be any code   to be executed at the end of each loop.
        •  
         

        Note: Each of the parameters can be empty or have multiple expressions  separated by commas.

         
           
        • cond: All expressions separated by a comma are evaluated but the   result is taken from the last part. This parameter being empty means the   loop should be run indefinitely. This is useful when using a conditional   break statement inside the loop for ending the loop.

    1 more annotation...

    • Server Side Includes

       

      You can insert the content of a file into a PHP file before the server  executes it, with the include() or require() function. The two functions are  identical in every way, except how they handle errors. The include() function  generates a warning (but the script will continue execution) while the require()  function generates a fatal error (and the script execution will stop after the  error).

       

      These two functions are used to  create functions, headers, footers, or elements that can be reused on multiple  pages.

    • Now, let's assume we have a standard menu file that should be used on all  pages (include files usually have a ".php"  extension). Look at the "menu.php" file below:

       
       
      <html> <body>
       
      <a href="http://www.w3schools.com/default.php">Home</a> | <a href="http://www.w3schools.com/about.php">About Us</a> |  <a href="http://www.w3schools.com/contact.php">Contact Us</a>
       
       

      The three files, "default.php", "about.php", and "contact.php"  should all include the "menu.php" file.  Here is the code in "default.php":

       
       
      <?php include("menu.php"); ?>
       
      <h1>Welcome to my home page</h1>
       
      <p>Some text</p>
       
      </body> </html>
       
       

      If you look at the source code of the "default.php" in a browser, it will look something like  this:

       
       
      <html> <body> <a href="default.php">Home</a> | <a href="about.php">About Us</a> |  <a href="contact.php">Contact Us</a> <h1>Welcome to my home page</h1> <p>Some text</p> </body> </html>
       
       

      And, of course, we would have to do the same thing for "about.php" and "contact.php".  By using include files, you simply have to update the text in the "menu.php"  file if you decide to rename or change the order of the links or add another web page to the site.

    3 more annotations...

    • The PHP Date() Function

       

      The PHP date() function formats a timestamp to a more readable date and time.

    • Syntax

       
       
      date(format,timestamp)
       
       
                       
      ParameterDescription
      formatRequired. Specifies   the format of the timestamp
      timestampOptional. Specifies a timestamp. Default is the current   date and time (as a timestamp)

    6 more annotations...

    • The $_POST variable is used to collect values from a form with  method="post".
    • Information sent from a form with the POST method is invisible to others and has  no limits on the amount of information to send.

    2 more annotations...

    • The $_GET variable is used to collect values from a form with  method="get".
    • Information sent from a form with the GET method is visible to everyone (it will  be displayed in the browser's address bar) and it has limits on the amount of  information to send (max. 100 characters).

    4 more annotations...

    • Opening a File

       

      The fopen() function is used to open files in PHP.

       

      The first parameter of this function contains the name of the file to be opened and the  second parameter specifies in which mode the file should be opened:

       
       
      <html> <body>
       
      <?php $file=fopen("welcome.txt","r"); ?>
       
      </body> </html>
    • Note: If the fopen() function is unable to open the  specified file, it returns 0 (false).

    4 more annotations...

1 - 10 of 10
20 items/page
List Comments (0)