What is Static variable in PHP ?

Normally when a function is terminates then all the variable inside the function loose their value. But if we want to hold those value for the feature works, then we need do use static variable. Generally those variables which holds the values are called static variables inside a function. To doing this you must write static keyword just before variable. let’s see two example one is normal variable and another is static.

Variable without static

<?php
    function test_variable()
    {
        $x = 1;
        echo $x;
        $x++;
    }
    test_variable();
    echo "<br>";
    test_variable();
    echo "<br>";
    test_variable();
?>

Output

1
1
1

 

Variable with static

<?php
    function test_count()
    {
        static $x=1;
        echo $x;
        $x++;
    }
    test_count();
    echo "<br>";
    test_count();
    echo "<br>";
    test_count();
?>

Output

1
2
3

SO, Hope you guys enjoy it…..

What is PHP Ternary Operator

PHP Ternary Operator is the Shorthand of if/else statement. It’s is important to know “What is it, How to use it ” for php programmer.

It’s simply said that the shorthand of if/else statement. It’s rally easy to use and reduce you code.

Ternary Operator using “(condition) ? (return true value) : (return false value)” for execute the statement.It’s called Ternary Operator because it’s use three operands, one condition, one result for true, one result for false. Hear is an example.

<?php
    $age=10;
    $Status= ($age<18)? “Child”:”Adult”;
    echo $Status;
?>

Output

Child

NB:  The most important thing of using it reduce your code ,save the executing time.Makes maintaining code quicker, easier.

 

Image Uploader Script in PHP

Recently I am developing my component library to make my library rich. Because it’s reduce my production time. For this reason I have developed the image uploading class. Almost every application need image uploading part. So that I have decided to make a class to for image uploading. Which will is very easy to use and no need to extra settings. Just copy the script and use it. I have made it open source so anyone can use it.

<?php
/*
* Image Upload Class
*/
class ImageUpload
{
    /**
     * @param null $Path         Custom path where uploaded image will be stored
     * @param null $MaximumSize  Custom Maximum size in (Bytes) will be passed.
     * @param array $AllowedType Custom extension type (JPG|GIF|PNG)
     * @param null $Option       Extra options as array where we can define 'Image File Name', blah blah blah
     *
     * @return string Here return/output will be a string (Message)
     */
    public function ImgUpload($Path = null, $MaximumSize = null, array $AllowedType = null, $Option = null)
    {
        if (isset($_POST['submit'])) {
            if (!empty($_FILES['ImgName']['name'])) {
                $ImageFile = $_FILES['ImgName'];
                if ($this->CheckSizeLimit($ImageFile, $MaximumSize)) {
                    if ($this->CheckImageType($ImageFile, $AllowedType)) {
                        if ($this->CheckPath($Path)) {
                            if ($this->CheckPermission($Path)) {
                                if (move_uploaded_file($ImageFile['tmp_name'], $Path . $ImageFile['name'])) {
                                    $result = 'Image has been Uploaded';
                                    return $result;
                                } else {
                                    return false;
                                }
                            } else {
                                return "Directory do not have write permission,";
                            }
                        } else {
                            return "Directory could not found,";
                        }
                    } else {
                        if (isset($AllowedType)) {
                            return "Please select " . implode('"/"', $AllowedType) . " Image";
                        } else {
                            return "Please select jpg/png/gif image";
                        }
                    }
                } else {
                    $result = "Image must me less then" . $MaximumSize . " byte";
                    return $result;
                }
            } else {
                $result = 'You do not select any file';
                return $result;
            }
        }
    }

    /**
     * @param $ImageFile        Image File which is image is selected for upload
     * @param null $MaximumSize Custom max image size passed
     *
     * @return bool Return true or false
     */
    function CheckSizeLimit($ImageFile, $MaximumSize = null)
    {
        if (isset($MaximumSize)) {
            if ($ImageFile['size'] < $MaximumSize) {
                return $ImageFile['size'];
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * @param $ImageFile         Image File which is image is selected for upload
     * @param array $AllowedType Custom extension type (JPG|GIF|PNG)
     *
     * @return bool Return true or false
     */
    function CheckImageType($ImageFile, array $AllowedType = null)
    {
        if (isset($AllowedType)) {
            $i = 0;
            while ($i < sizeof($AllowedType)) {
                if ($ImageFile['type'] == "image/" . $AllowedType[$i]) {
                    return true;
                    break;
                }
                $i++;
            }
            return true;
        } else {
            if ($ImageFile['type'] == "image/jpg" || $ImageFile['type'] = "image/png" || $ImageFile['type'] == "image/gif") {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * @param null Custom path where uploaded image will be stored
     *
     * @return bool Return true or false
     */
    function CheckPath($Path = null)
    {
        if (isset($Path)) {
            if (is_dir($Path)) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    /**
     * @param null Custom path where uploaded image will be stored
     *
     * @return bool Return true or false
     */
    function CheckPermission($Path = null)
    {
        if (isset($Path)) {
            if (is_writable($Path)) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    protected function ImgSize($ImgFile = null)
    {
        return "OK";
    }
}

This is the class structure. you can get this code from github. There I have written more details about it. I hope this will be helpful for you development.