Some Very Useful Functions


Throughout this book, there a number of functions that PHP provides for us (also known as built-in functions) that we will use. We will introduce a few of the more useful among them now.

nl2br

You will have noticed from the various samples in this chapter that newline characters do not always correspond to line breaks on outputHTML clients (web browsers) format the text stream as they see it, following any existing processing instructions. When you wish to keep the newline characters in your string in the output, you can call the nl2br function, which converts any newline characters into a <br/> tag. For example, the following script

 <?php    $stringval = <<<EOSTRING    This is a string with    lots of newline breaks    and I want it to look like this    on    output. EOSTRING;   echo nl2br($stringval); ?> 

produces the following output:

 This is a string with lots of newline breaks and I want it to look like this on output. 

var_dump

There are times when you will be experimenting or writing a program and you will want to see the contents of a variable. You might also find the dynamic nature and lack of explicit type declaration of PHP variables, meaning you are entirely uncertain as to the variable's current type. The var_dump function prints the type and the value of a variable to the output stream. (In fact, you can give it more than a variableit takes the result of any valid PHP expression.) For strings, var_dump even provides the number of characters in the string.

 <?php $floatVar = 123e-456; $intVar = 123456; $stringVar = "Please confirm your shipping address:"; var_dump($floatVar); echo "<br/>\n"; var_dump($intVar); echo "<br/>\n"; var_dump($stringVar); echo "<br/>\n"; ?> 

The previous code generates the following output in a web browser:

 float(0) int(123456) string(37) "Please confirm your shipping address:" 

print_r

The print_r function is similar to var_dump, but also endeavors to make the output human readable. print_r even lets you give it an optional value (called a parameter) that tells it to return the results in a string rather than print them to the output stream.

The user-friendly output is particularly useful for objects and arrays, which you will see in later chapters.

 <?php $stringVar = "We proudly serve Coffeetastic coffee."; print_r($stringVar); echo "<br/>\n"; $result = print_r($stringVar, TRUE); echo $result; ?> 

The previous script prints the following output in a web browser:

 We proudly serve Coffeetastic coffee. We proudly serve Coffeetastic coffee. 

var_export

The last in the little cluster of printing functions is the var_export function, which is extremely similar to var_dump, except that the output is actually a valid PHP code representation of the provided data's values.

For example, the following script

 <?php   $arr = array(1, 2, 3, 4);   var_export($arr); ?> 

produces the following output in our web browser (which we have formatted for spacing):

 array (   0 => 1,   1 => 2,   2 => 3,   3 => 4, ) 




Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net