PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: undefined variable
undefined variable - phponwebsites.com
Showing posts with label undefined variable. Show all posts

16 Jun 2014

4 Types of errors in php

                       Errors will occur for following reason in php:
                              1.unclosed quotes,
                              2.missing semicolon,
                              3.missing parentheses,
                              4.add extra parentheses,
                              5.unclosed braces,
                              6.undefined variable,
                              7.call undefined function,
                              8.includes files which is not found

Common errors in php:


                    Basically four types errors is occurred in php. There are:

                              1. Parse error
                              2. Fatal error
                              3. Warning
                              4. Noticed error

Parse error:

                       
                             The parse errors php is the syntax error. It stops the execution of the script in php. The common reasons for occur parse in php as follows:
                              1.unclosed quotes
                              2.missing semicolon
                              3.unclosed braces
                              4.missing parentheses
                              5.add extra parentheses

Parse error by unclosed quotes in php:

                               
                           Consider the following example in php:

<?php
          $name='guru;
          echo $name;
?>

       where, the quotes are not closed. When you run this file, you'll get error message like this:

parse error by missing quotes in php

Parse error by missing semicolon in php:

                                     Consider the following example:

<?php
     $name='Guru'
     echo $name;
?>

          where, the semicolon is missing at $name. It gives the parse error like below:

Parse error by missing semicolon in php

Parse error by unclosed braces in php:


<?php
 $a=5;
 $b=6;
 if($a==$b)
 {
   echo 'equal';
 else
 {
   echo'Not equal';
 }
?>

     where the closing brace is missed. When you run this file, you'll get error message like below:

parse error by missing braces in php

Fatal error in php:


                The fatal error will occur when you call the undefined function in php. It stop the execution of the php script.

<?php
 function name($str)
 {
  echo $str;
 }
  echo place('guru');
?>

            where the function "place()" is not defined. But you call that function. Now it gives the fatal error like below:

fatal error in php

Waring in php:


                       The warning is occurred when you include the file but the file is not found in php. It does not stop the execution of the php script.

<?php
 include('welcome.php');
 echo'welcome'; 
?>

       where, the file welcome.php is not found but you include it. So you'll get output like this:

warning error in php

Noticed error in php:


                   The noticed error is occurred when you use undefined variable. It does not stop execution of the php script.

<?php
 $name='Guru';
 echo $guru;
?>

       where $guru is not defined but you accessed. So you'll get error message like this:

Noticed error in php

22 May 2014

PHP - Session

                      Session is the temporary memory to store a particular value. All of you use gmail, that gmail is the bset example for session concept. If you login gmail, then close the browser. Now you open gmail in your browser, you login gmail again. Unlike, you logout from gmail and close the browser. Now you can't login again. It comes to login page. This is the session concept. The value is stored in session until you destroy the session.

Session in php:


                      In php, there is 3 steps for session.
                      1. Start the session using session_start().
                      2. Load values to session variable by $_SESSION['name']='Guru'.
                      3. Destroy the session by session_destroy() or unset($_SESSION['name']).

session_start() in php:


                      It is used to start the session in php. Consider folowing example in php:

<?php    
         session_start();
         $_SESSION['name']='Guru';    
         echo $_SESSION['name'];
?>

                     Now you'll get output:     Guru

Suppose you run file without session_start(). Then you'll get error message like below:

session_start() in php

session_destroy() in php: 


                   It is used to destroy the session. Consider following example in php:
welcome.php:

<?php    
         session_start();
         $_SESSION['name']='Guru';    
         echo $_SESSION['name'].'<br>';
         echo'<a href="signout.php">Signout</a>';
?>

signout.php:

<?php    
         session_start();
         session_destroy();
         header('location:welcome.php');
?>

                When you run the welcome.php, your output like this:

                       Guru
                       Signout

                  When you click the signout, it destroy the session and navigates to welcome.php. Now you try to clikc back button on your browser, it go to welcome.php page, but the value stored in session is not displayed. It give error message like below:

session_destroy() in php

unset() in php:


                  You can also delete the session using unset() in php. The script for unset() as follows as:

<?php    
         session_start();
         unset($_SESSION['name']);
         header('location:welcome.php');
?>

It is also give same output like session_destroy() in php.

Related Post: