Skip to main content

PHP

PHP Programs for Practice
1. An example of Variables.
            <?php
                        $str=”Hello World”;
                        $X=10;
                        $y=15.75;
                        echo $str,”<br/>”;
                        echo $X, “<br/>”;
                        echo $y;
            ?>
2. An example of assigning values to Variables.           
            <?php
                        $num=22;
                        $square=$num * $num;
                        $script='PHP';
                        echo "<br/>Script $script";
                        echo "<br/>Number $num";
                        echo "<br/>Square $square";
            ?>
3. An example of Reference Assignment.
        <?php
        $script1="ASP";
         $script2=&$script1;
        echo "$script1<br/>";
         echo "$script2<br/>";
        $script2="PHP";
        echo "$script1<br/>";
        echo "$script2<br/>";
    ?>
      
4. An example of Variable of Variable.
    <?php
         $varname='number';
        $$varname=5;
        echo $varname;
        echo "<br/>";
        echo $number;
        echo "<br/>";
    echo $$varname;
    ?>

5. An example of Global Variable.
        <?php
            $x = 10;
            function printx() 
            {
GLOBAL $x; //Global Variable
                        $x = 0;
                        print "X inside function is $x. <br>";
            }
            printx();
            print "X outside of function is $x. <br>";
    ?>
6. An example of Static Variable.
        <?php
         function countn()
    {
         STATIC $c=0;
    $c++;
    echo "<br/>Call ".$c;
    }
    countn();
    countn();
    countn();
        ?> 

7. An example to Remove Variable.
    <?php
$script='PHP';
echo $script;
unset($script);
echo $script;
    ?>

8. An example to  define a constant using define().

    <?php

            define("script","PHP Script");

            echo script; //Constant is accessed without $ symbol

?>

9. An example to  define a constant using const.

    <?php

            const shape="Circle";

            echo shape;

    ?>

Const

Define()

Const defines the constants at compile time.

Define() defines the constants at run time.

Const cannot define constants conditionally.

Define() can be used define constants conditionally.

Constants defined by const are case-sensitive.

Constants defined by define() can be case-insensitive.

Const takes static scalar values (number, string or other constant like true, false, null) 

Whereas define() accepts any expression as name.

const defines a constant in the current namespace.

define() has to be passed the full namespace.


10. An example to  prove constants are global.

    <?php
            define("SCRIPT", "PHP Script");
            function myScript()
            {
                echo SCRIPT;
            }
      myScript();
            echo "<br/>";
        echo SCRIPT;
    ?>
11.  Predefined Constants

A. __LINE__ Constant: -This constant returns the current line number of the file. It returns the line number in the source file where it is used.

<?php
echo "Good Morning <br/>";
echo "<br/>Line Number ".__LINE__;
echo "<br/>Line Number ".__LINE__;
echo "<br/>Line Number ".__LINE__;

?>

B. __FILE__ Constant:-This constant returns the full path and the name of the source file that’s being executed.

<?php
echo "<b>Displaying the name of the File <br/>";
echo "<br/>File Path and Name ".__FILE__;
?>

C. __DIR__ Constant: -This constant returns the name of the directory in which source file is stored. It returns the path without file name.

<?php
echo "<b>Displaying the Directory of the File <br/>";
echo "<br/>File Path and Directory".__DIR__;
?>

D. __FUNCTION__ Constant: -This constant returns the name of function in which it is used.

<?php
echo "<b>Displaying the Function Name</b><br/>";
function fun()
{
echo "<br/>The Name of function is : - ".__FUNCTION__;
}
fun();
?>

5. __CLASS__ Constant: -This constant returns the name of the class in which it is used.

<?php
class myClass
{
public function showName()
{
echo __CLASS__;
}
}
$nm=new myClass();
echo "The name of the class : - ";
$nm->showName();
?>

E. __METHOD__ Constant: -This constant returns the name of class method in which it is used.

<?php
class myClass
{
public function showName()
{
echo __METHOD__;
}
}
$nm=new myClass();
echo "The name of the class method : - ";
$nm->showName();
?>

7. __NAMESPACE__ Constant: -This constant returns the name of the namespace used in the source code.
<?php
namespace MyNameSpace;
echo "The namespace used in this program - ", __NAMESPACE__;
?>







Comments

Popular posts from this blog