Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
        }
?>

Then save this script as echo_i.php:


Click here to view code image
<?php
echo $i;
?>


If you run include1.php, PHP loops from 10 to 0 and includes
echo_i.php each time. For its part, echo_i.php just prints the value of
$i, which is a crazy way of performing an otherwise simple operation, but it
does demonstrate how included files share data. Note that the include
keyword in include1.php is inside a PHP block, but you reopen PHP
inside echo_i.php. This is important because PHP exits PHP mode for
each new file, so you always have a consistent entry point.


Basic Functions


PHP has a vast number of built-in functions that enable you to manipulate
strings, connect to databases, and more. There is not room here to cover even
10% of the functions. For more detailed coverage of functions, check the
“References” section at the end of this chapter.


Strings


Several important functions are used for working with strings, and there are
many more of them that are less frequently used that we do not cover here.
We look at the most important functions here, ordered by difficulty—easiest
first!


The easiest function is strlen(), which takes a string as its parameter and
returns the number of characters in there, like this:


Click here to view code image
<?php
$ourstring = " The Quick Brown Box Jumped Over The Lazy Dog ";
echo strlen($ourstring);
?>


We use this same string in subsequent examples to save space. If you execute
this script, it outputs 48 because 48 characters are in the string. Note the 2
spaces on either side of the text, which pad the 44-character phrase up to 48

Free download pdf