Learn PhP By Code

PHP - Echo example

           <?php
             $myString = "Hi! This is a test";
              echo $myString;
             echo "<h1>PHP!</h1>";
           ?>


PHP - Strings example
    
    <?php
    $test_string = "tips-tech";
    echo "tech tips- php";
    echo $test_string;
    ?>


PHP - Syntax  example

     <?php
     ?>

    Hi this  is php

     <?
     ?>

PHP - Include File example

      <?php include("test.php"); ?>
          <p>How test.php include  to  a  php page </p>
          

PHP- Defines Constants

             <?php
              define("MYTEXT", "hi!frnd");
             echo MYTEXT;
            ?>

PHP If...Else loop

      <?php
             $k=1;
          if ($k==1)
          {
          echo "k is 1";
          }
          else
          {
          echo "k  is  not 1!";
          }
     ?>           

PHP  Switch loop

       <?php
             $txt="read";
            switch ($txt)
            {
              case "read":
               echo "Your are reading!";
                break;

              case "Writing":
               echo "Your are Writing!";
               break;
           
             default:
               echo "Your are seeping!";
        }
       ?>

PHP File demo code

   <?php
       $ft = fopen("testfile.txt", "r");
    ?>

    

   <?php
      $tsFile = "File.txt";
      $ft = fopen($tsFile, 'r');
      $fData = fread($ft, 5);
       fclose($ft);
      echo $fData;
   ?>


PHP File Upload

    <form enctype="multipart/form-data" action="upload_page.php" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
         file to upload:
           <input name="loadedfile" type="file" /><br />
          <input type="submit" value="Upload" />
      </form>

    
   
    multipart/form-data is used when a form requires binary data, like the contents of a file for uploader
  
     <?php
        
         $target_path = "upload-store/";
         $target_path = $target_path . basename( $_FILES['loadedfile']['name']);
         $_FILES['loadedfile']['tmp_name'];
     ?>    
  
    <?php
    if(move_uploaded_file($_FILES['loadedfile']['tmp_name'], $target_path))
     {
     echo "The file ". basename( $_FILES['loadedfile']['name'])." uploaded Successfully";
     }
     else
      {
       echo "error!!!!!";
       }

     ?>



PHP Session
  
    session_start() function is used for start the session.
   
    <?php
     session_start();
     $_SESSION['testname'] = "Lisa"; // records the session data
      echo "Hi! = ". $_SESSION['testname']; //get session data of $_SESSION['testname']
      ?>