Lab06-php03 V1.01 ---updated

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

LAB 06

Php

Arrays

1. Integer indices - array


<?php
$stuff = array("Hi", "There");
echo $stuff[1], "\n";
?>

2. Key/Value – array
<?php
$stuff = array("name" => “Chew",
"course" => “Web Application");
echo $stuff["course"], "\n";
?>

3. Dumping array, screenshot the output


- Print_r ($stuff)
- Var_dump($stuff)

echo("<pre>\n");
print_r($stuff);
echo("\n</pre>\n");

var_dump($stuff);
4. For loop, to loop through array.
<?php
$stuff = array(“Chew",“WebApp");
for($i=0; $i < count($stuff); $i++) {
echo "I=",$i," Val=",$stuff[$i],"\n";
}
?>

5. Task
- Create an array to store at least 4 items. (e.g., banana, apple, pineapple, mango).
- Use a loop (for or while), to loop through the array and print the item.
Super Global $_GET (Array)
Php – end of url parameters into a global variable name $_GET

1. Try out the following code to dump out the variable from url parameters.

<h1>Contents of the $_GET array</h1>


<p>Using print_r:</p>
<pre>
<?php
print_r($_GET);
?>
</pre>
<p>Using var_dump:</p>
<pre>
<?php
var_dump($_GET);
?>
</pre>

2. Access the php code above through the url from the localhost, type in your name as
one of the parameters in the url, observe whether your name has been printed on the
browser.
- Observe that the code will dump the parameters in an array.
- The parameters will be stored in a global variable $_GET. Screenshot the output
Functions

1. Playing with built in function.


echo strrev(" .dlrow olleH");
echo str_repeat("Hip ", 2);
echo strtoupper("hooray!");
echo strlen("intro");
echo "\n";

Reference: https://www.php.net/manual/en/ref.strings.php
Reference: https://www.w3schools.com/php/php_string.asp

2. Use str built in function to calculate the length of your full name. Screenshot the
output

3. Defining your own function. Try out the following code.

function greet() {
print "Hello\n";
}
greet();
greet();

4. Write the following for loops in a function. Call the function for at least 2 times.
Screenshot the output.

for($i=0; $i < 10; $i++) {


echo "I= ", $i,"\n";
}
Call by Value / Call by Reference
1. Call by value. Understand the difference between call by value and call by reference.

function double($alias) {
$alias = $alias * 2;
return $alias;
}
$val = 10;
$dval = double($val);
echo "Value = $val Doubled = $dval\n";

2. Call by reference. Understand the difference between call by value and call by
reference.

function triple(&$realthing) {
$realthing = $realthing * 3;
}
$val = 10;
triple($val);
echo "Triple = $val\n";
Optional: Function

1. Basic return function. Try out the following code.

function greeting() {
return "Hello";
}
print greeting() . " Glenn\n";
print greeting() . " Sally\n";

2. Using the basic return function given above, store the return value of function into a
variable, and print the variable out on a browser.

3. Parameters and arguments. Try out the following code.

function howdy($lang='es') {
if ( $lang == 'es' ) return "Hola";
if ( $lang == 'fr' ) return "Bonjour";
return "Hello";
}
print howdy('es') . " Glenn\n";
print howdy('fr') . " Sally\n";

4. Using the function given in the previous section. Add an additional language (e.g,.
Chinese). Then call the function to print it out on to the browser.
Optional: Global Variable

1. Normal scope.
function tryzap() {
$val = 100;
}
$val = 10;
tryzap();
echo "TryZap = $val\n";

2. Global scope
function dozap() {
global $val;
$val = 100;
}
$val = 10;
dozap();
echo "DoZap = $val\n";

3. Try out the code, and try to understand how does a global variable work.

You might also like