Friday, April 19, 2013

PHP Notes - I

Here I am discussing those small tidbits of PHP which we may not use in our PHP projects but they are very much part of core PHP. It is good to learn and remember them. I am discussing them point-wise below.

1. In PHP, variables containing Integer, Float, String or Boolean data are called Scalar variables. But variables with Null, Object, Resource or Array data won't be treated as scalar variable. We can test whether a variable is of scalar type by use of is_scalar() function.
 

<?php
$p = 100;
echo is_scalar($p);    // 1
echo is_scalar("");    // 1
echo is_scalar(false); // 1
?>

 

2. Conversion from other built-in types to objects creates stdClass objects. Check out the code below.
 

<?php
$v = (object)"standard";
var_dump( $v);
?>


Here one string ["standard"] has been converted to an Object. Check out the output below ::

object(stdClass)#1 (1) { ["scalar"]=> string(8) "standard" }

An object of built-in class stdClass is created on the fly, and its member called "scalar" holds the string "standard". Check out the another code snippet below.

<?php
$fp = fopen("test.txt", "r+");
$v = (object) $fp;
var_dump($v);
echo "<br>";
 

$v = (object) NULL;
var_dump($v);
echo "<br>";
 

$v = (object) array(1,2,3,4);
print_r($v);
?>


Output ::

object(stdClass)#1 (1) { ["scalar"]=> resource(3) of type (stream) }
object(stdClass)#2 (0) { }
stdClass Object
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
)


The first var_dump() shows that the typename of variable $fp (Resource) is stored in scalar member of stdClass object. The second var_dump() call shows an empty object when NULL was converted to object. However, this empty object does not result to be TRUE on echo empty($v); call. The print_r ($v) in the last line shows the object in array format. However we can not access each member in that object as $v->0 or $v->1 etc. Let's convert an associative array to object.

<?php
$v = (object) array("t1"=>1,"t2"=>2,"t3"=>3,"t4"=>4);
echo $v->t1 . $v->t2 . $v->t3 . $v->t4;
?>

Here, everything simply goes fine. Four members can be accessed as $v->t1, $v->t2, $v->t3, $v->t4.


3. Check out some predefined ("magic") constants used in PHP in program written below.

<?php
    class di_splay
    {
       public $name = 'Class One';
      
       function disp_lay()
       {
        echo "This PHP file is in Directory :: " . __DIR__ . "<br>";
        echo "This PHP file's full path :: " . __FILE__ . "<br>";
        echo "The name of this Function :: " . __FUNCTION__ . "<br>";
        echo "The name of this Class :: " . __CLASS__ . "<br>";
        echo "The name of this Method :: " . __METHOD__ . "<br>";
        echo "Curently Executing Line No. :: " . __LINE__ . "<br>";
       }
    }

   $di_splay = new di_splay;
   $di_splay->disp_lay();

?>


Output ::

This PHP file is in Directory :: C:\xampp\htdocs
This PHP file's full path :: C:\xampp\htdocs\test.php
The name of this Function :: disp_lay
The name of this Class :: di_splay
The name of this Method :: di_splay::disp_lay
Curently Executing Line No. :: 13

Hence the usage of constants like __DIR__, __FILE__, __FUNCTION__ is quite clear in the example above. I want to show another important point here ergarding __FILE__ constant. Check out the example below.

content of test.php ::
<?php
  ///// test.php
  include("include/test2.php");
   
  echo "This file names :: " . __FILE__ . "<br>";
  echo "This dir  names :: " . __DIR__ . "<br>";
?>


content of test2.php ::
<?php
  ///// test2.php

 
  echo "INCLUDED file names :: " . __FILE__ . "<br>";
  echo "INCLUDED dir  names :: " . __DIR__ . "<br>";
?>


Output :: 


INCLUDED file names :: C:\xampp\htdocs\include\test2.php
INCLUDED dir names :: C:\xampp\htdocs\include
This file names :: C:\xampp\htdocs\test.php
This dir names :: C:\xampp\htdocs

This clearly shows that __FILE__ and __DIR__ show filename and path of file in which they are contained. Though test2.php was included in test.php, __FILE__ in test2.php shows test2.php's properties.

No comments: