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

  • 1 Replies
  • 1256 Views
*

Sharmait007

  • Newbie
  • *
  • 8
  • +0/-0
    • View Profile
Learn Php_dump() | Php upload file | Php string functions
« on: September 09, 2011, 03:35:14 AM »
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
« Last Edit: September 30, 2011, 08:28:29 AM by Webhelpforums »

*

seamustaylor

  • Newbie
  • *
  • 2
  • +0/-0
    • View Profile
Re: Learn Php_dump() | Php upload file | Php string functions
« Reply #1 on: September 30, 2011, 12:27:20 AM »
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;=========";
    }
}
« Last Edit: September 30, 2011, 08:29:09 AM by Webhelpforums »

 




See Our Webmaster Tools for Windows and Mac

A1 Sitemap Generator
      
A1 Website Analyzer
      
A1 Keyword Research
      
A1 Website Download
      
A1 Website Search Engine
      
A1 Website Scraper