Showing posts with label print. Show all posts
Showing posts with label print. Show all posts

Thursday, September 11, 2008

The print_r() function

We know that to print individual elements of an array, the echo() of the print() function could be used.
However, to print an array as such, we must use the print_r() function.
Syntax: print_r($expression, $return);

$expression the expression to be printed.
$return
if this parameter is set to TRUE, the print_r() function would return its output to a variable.
if this parameter is FALSE, the output would be printed. FALSE is the default value.

Also, remember that the print_r() function is not used only for printing arrays, but also for any mixed expressions.
Example input:
1.
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee);
echo "
";
print_r($sum = 100 + 300);?
2.?
$employee = array(array('John', 223,42),
array('Daisy',443,50),
array('Mathew',990,37)
);
print_r($employee,true);
echo "
";
echo $str;
Example output:
1.
Array ( [0] => Array ( [0] => John [1] => 223 [2] => 42 ) [1] => Array ( [0] => Daisy [1] => 443 [2] => 50 ) [2] => Array ( [0] => Mathew [1] => 990 [2] => 37 ) ) ?400??
2.
Array ( [0] => Array ( [0] => John [1] => 223 [2] => 42 ) [1] => Array ( [0] => Daisy [1] => 443 [2] => 50 ) [2] => Array ( [0] => Mathew [1] => 990 [2] => 37 ) )

Sunday, September 7, 2008

The print Function

The print function is used to print one or more variable in the output.
Syntax: print variablename;

Example inputs:
1.
print “smith”;
2.
$name = “Jones”;
print $name;
3.
$name=”Natasha”;
print “My name is $name”;
print “
”
print “What is yours?”;

Example outputs:

1.
Smith
2.
Jones
3.
My name is Natasha
What is yours?

Some PHP Exercises

What does an echo function do?
What is the difference between using single quotes and double quotes in the echo function?

Friday, September 5, 2008

The echo function

The echo function is used to print one or more variable in the output.
Syntax: echo variable-name;
Example inputs
1.
echo “smith”;
2.
$name = “Jones”;
echo $name;
3.
$name=”Natasha”;
echo “My name is $name”;
echo “
”
echo “What is yours?”;
4.
$name = “Robin”;
echo ‘My name is $name’
5.
echo ‘My’,’name’,’is’,’John’;
Example outputs:
1.
Smith
2.
Jones
3.
My name is Natasha
What is yours?
4.
My name is $name
5.
My name is John