Learn Php_dump() | Php upload file | Php string functions

Started by Sharmait007, September 09, 2011, 03:35:14 AM

Sharmait007

File uploading:
You can use PHP to allow your users to upload a file to the server.
<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

Php string functions:
substr()
This function returns the part of the string as an output.
subrev()
This function returns the reverse of the string.
strlen()
This function returns the length of the string
trim()
This function removes the whitespaces from both start and the end of the string.
strtolower()
This function converts the string to lower case
strtoupper()
This function converts the string to upper case
str_replace()
The str_replace() function replaces some characters with some other characters in a string.
explode()
This function breaks the string into array on the basis of delimiter passed.
implode()
This function join array elements with a string on the basis of delimiter passed.

PHP Dump:
var_dump — Dumps information about a variable

seamustaylor

#1
The print_r() is a very valuable function for all PHP developers. However, there is two problems with this function -

    The argument must be an array
    The result is formated in in text/plain. In text/html, it will not be formatted.

To solve these problems, I have created a function in PHP that will call the print_r() function. It will put the code generated by the print_r function inside <pre> tags. That will make the data readable from the browser.

If the given argument is not an array it will take the necessary action according to its data type. If the argument is an array, print_r() is used. If the argument is a object, var_dump() is used - with <pre> formatting. If it is anything else, it is printed with a var_dump().


Sample Code

$data = array(
    'success'    =>    "Sweet",
    'failure'    =>    false,
    'array'      =>    array(),
    'numbers'    =>    array(1,2,3),
    'info'       =>    array(
                        'name'    =>    'Binny',
                        'site'    =>    'http://cegonsoftrecruitments.wordpress.com'
                 )
);

Result using print_r($data)
Array ( [success] => Sweet [failure] => [array] => Array ( ) [numbers] => Array (
  • => 1 [1] => 2 [2] => 3 ) [info] => Array ( [name] => Binny [site] => http://cegonsoftrecruitments.wordpress.com ) )

    If you view the source of this document, the above output will make a lot more sense. But if you are looking at it in a browser, it is much more difficult to understand it.
    Result using dump($data)

    -----------------------
    Array
    (
        [success] => Sweet
        [failure] =>
        [array] => Array
            (
            )

        [numbers] => Array
            (
               
  • => 1
                [1] => 2
                [2] => 3
            )

        [info] => Array
            (
                [name] => Binny
                [site] => http://cegonsoftrecruitments.wordpress.com/
            )

    )
    -----------------------

    dump(5);

    =========>int(5) <=========

    dump("Hello");

    =========>string(5) "Hello" <=========

    Code
    <?php
    /** Function : dump()
    * Arguments : $data - the variable that must be displayed
    * Prints a array, an object or a scalar variable in an easy to view format.
    */
    function dump($data) {
        if(is_array($data)) { //If the given variable is an array, print using the print_r function.
            print "<pre>-----------------------\n";
            print_r($data);
            print "-----------------------</pre>";
        } elseif (is_object($data)) {
            print "<pre>==========================\n";
            var_dump($data);
            print "===========================</pre>";
        } else {
            print "=========&gt; ";
            var_dump($data);
            print " &lt;=========";
        }
    }