Categories
Uncategorized

Day 3-4: Revising PHP

It’s been years since I last touched PHP. I’ve learnt at least 10 different languages since then, so there’s obviously a need to visit the syntax of PHP again. I’m done revising it now by doing some hands-on, and following are the files:

General Syntax:

<?php
    echo 'First code of PHP<br>'; //echo
    $var = 'Variable declared and assigned'; //variable
    echo $var.' concat'; //concatenation
    unset($var); //destroy var
    $name = 'Kirito';
    $age = 23;
    echo '<br>';
    var_dump($age); //information about a variable
    echo '<br>';
    var_dump($name);
    $boolVar = true;
    var_dump($boolVar);

    echo '<br>$$ example<br>';
    $name="Rajeev";     //variable 'name' with value 'rajeev'

    $$name="Sanjeev";   //variable 'rajeev' with value 'sanjeev'

    echo $name."<br/>";

    echo $name."<br/>";

    echo $Rajeev."<br/>";

    define('CONSTVAR', 20);     //constant

    //Magic Constants
    echo __LINE__.'<br/>';
    echo __FILE__.'<br/>';
    echo PHP_VERSION.'<br/>';
    /*
    Not applicable in this file, but in case of class/functions:
    __FUNTION__
    __CLASS__
    __METHOD__
    */

    gettype($name); //get data type

    //arrays
    $arr = array(1, 2, 3);

    //append
    $arr[ ] = 4;
    for ($i=0; $i < count($arr); $i++) { 
        echo $arr[$i];
    }

    foreach($arr as $x){
        echo $x;
    }
    var_dump($arr);

    //multiple data types allowed
    $ar2 = array(1, 'KP');
    var_dump($ar2);

    //associative array
    echo '<br/>';
    $assoarr = array(1=>2, "KP"=>"PHP");
    $assoarr[2] = "Hi";
    foreach($assoarr as $key=>$val)
    {
        echo $key.": ".$val."<br/>";
    }

    //array iterator
    $iterator = new ArrayIterator($assoarr);
    while($iterator->valid())
    {
        echo "<br/>". $iterator->current() . " at " . $iterator->key();
        $iterator->next();
    }

    //create array from 10 to 1 with step = 1
    $arrRange = range(10, 1, 1);
    foreach($arrRange as $x)
    {
        echo '<br/>'. $x;
    }

    //select a random element from array arrRange
    $randomNum = array_rand($arrRange);
    echo '<br/>'. $randomNum;
    //class
class Demo
{
    public $num = 3;
    function __construct($n)
    {
        $this->num = $n;
    }
    function show()
    {
            echo '<br>Shwo called<br>';
    }

    function __destruct()
    {
        echo "<br/>Destruct<br/>";
    }
}

    $obj = new Demo(5);
    $obj->show();
    echo '<br/>'.$obj->num. "<br/>";
    unset($obj); //destruct the obj
    //null data type
    $blank = null;

    //resource data type
    //$con = mysqli_connect("localhost","root","","users");

    //functions to check data type
    is_int($var);
    is_nan($var);
    is_null($var);
    is_long($var);
    is_float($var);
    is_bool($var);
    //is_file()
    is_array($arr);
    is_object($obj);
    is_numeric($var);
    is_string($name);


    //cookie
    //name of cookie, value, time before expiry
    setcookie("user", "kp", time()+60*60);

    //mail
    // $headers .= "From:info@phptpoint.com";

    // $headers .= "Content-type: text/html; charset=iso-8859-1rn";

    //mail($to, $subject, $message, $headers);



?>

Example Login

<?php
		
error_reporting(1);
		
$id = $_POST['id'];
		
$pass = $_POST['pass'];
		
if(isset($_POST['signin'])) {	
    if($id=="kp" && $pass=="kp")           
    {
        header('location:https://www.phptpoint.com');           
    }
    else
	{
        echo "<font color='red'>Invalid id or password</font>";		
    }
		
}
	
?>

<body>
    <form method="post">
        <table border="1" align="center">
            <tr>
                <td>Enter Your Id</td>
                <td><input type="text" name="id"/></td>
            </tr>
        
            <tr>
                <td>Enter Your Password</td>
                <td><input type="password" name="pass"/></td>
            </tr>

            <tr>
                <td><input type="submit" name="signin" value="SignIn"/></td>
            </tr> 
        </table">
    </form>
</body>

OOP:

<?php

class BaseClass
{
    private $_num;
    function __construct($n)
    {
        $this->_num = $n;
        echo "<br>Constructor of base called.<br>";
    }

    public function kp($var)
    {
        echo "<br>kp: ".$var."<br>";
    }

    final public function tp()
    {
        echo "<br>This function cannot be overridden because of final keyword.<br>";
    }

    function __destruct()
    {
        echo "<br>Destructor of base called.<br>";
    }
}

final class DerivedClass extends BaseClass //final keyword here means that this class cannot be extended
{
    var $nn;
    function __construct($n)
    {
        parent::__construct($n);
        $this->nn = $n * 2;
    }

    // public function kp()
    // {
    //     echo "<br>kp called with derived" . "<br>";
    // }
}

$derObj = new DerivedClass(4);
$derObj->kp(5);
echo "<br>".$derObj->nn."<br>";
unset($derObj);
?>

Leave a comment

Design a site like this with WordPress.com
Get started