0% found this document useful (0 votes)
7 views9 pages

Lab Activity 4

This document outlines a PHP lab activity focusing on string manipulation and array usage. It includes various tasks such as transforming strings, using string functions like strlen() and strstr(), and working with indexed, associative, and multidimensional arrays. Each activity provides step-by-step procedures for coding and expected outcomes to enhance understanding of PHP scripting.

Uploaded by

nurulshakierah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

Lab Activity 4

This document outlines a PHP lab activity focusing on string manipulation and array usage. It includes various tasks such as transforming strings, using string functions like strlen() and strstr(), and working with indexed, associative, and multidimensional arrays. Each activity provides step-by-step procedures for coding and expected outcomes to enhance understanding of PHP scripting.

Uploaded by

nurulshakierah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LAB ACTIVITY 4: Construct String and Arrays in PHP

Environment.

Duration: 3 Hours

Learning Outcomes

By the end of this tutorial session, you should be able to:

1. Use strings function in PHP scripts.


2. Use loops and function in array statement.
a. Indexed Array.
b. Associative Array.
c. Multidimensional Array.

Hardware/ Software:
Code Editor (e.g: Notepad++, sublime, Dreamweaver), server bundle (e.g: XAMPP)

Activity 4A
Activity Outcome: Use strings function in PHP scripts.

i. Transform a string to uppercase letters.


ii. Transform a string to lowercase letters.
iii. Change a string's first character to uppercase.
iv. Change a string's first character of all the words to uppercase.

Procedure:

Step 1: Open a PHP Editor and type the following code:


<html>
<body>

<?php
//all uppercase letters
echo (strtoupper("web programming is awesome."))."<br>";
//all lowercase letters
echo (strtolower("WEB PROGRAMMING IS AWESOME."))."<br>";
//change a string's first character to uppercase
echo (ucfirst("web programming is awesome."))."<br>";
//change a string's first character of all the words to uppercase
echo (ucwords("web programming is awesome."))."<br>";
?>

</body>
</html>

Step 2: Save as Lab4a.php to the document root and browse it.


Step 3: Write the output and explain your observation.

Activity 4B
Activity Outcome: Use strlen() function in PHP scripts.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<html>
<body>
<?php
//Return the length of the string
echo strlen("WEB PROGRAMMING IS AWESOME");
?>
</body>
</html>

Step 2: Save as Lab4b.php to the document root and browse it.

Step 3: Write the output and explain your observation.

2
Activity 4C
Activity Outcome: Use strstr() function in PHP scripts

Procedure:

Step 1: Open a PHP Editor and type the following code:

<html>
<body>
<?php
//Find the first occurrence and return the rest of the string
echo strstr("WEB PROGRAMMING IS AWESOME","PROGRAMMING");
?>
</html>
</body>

Step 2: Save as Lab4c.php to the document root and browse it.

Step 3: Write the output and explain your observation.

Activity 4D
Activity Outcome: Use strpos() function in PHP scripts

Procedure:

Step 1: Open a PHP Editor and type the following code:

<html>
<body>

<?php
//Find the position of the first occurrence of word “ ” inside the string
echo strpos("I love web programming","web");
?>

</html>
</body>

Step 2: Save as Lab4d.php to the document root and browse it.

Step 3: Write the output and explain your observation.

3
Activity 4E
Activity Outcome: Use substr() function in PHP scripts.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<html>
<body>

<?php
// Positive numbers:
echo substr("I love web programming",0,10)."<br>";
echo substr("I love web programming",1,8)."<br>";
echo substr("I love web programming",0,5)."<br>";
echo "<br>";

// Negative numbers:
echo substr("I love web programming",0,-1)."<br>";
echo substr("I love web programmingd",-10,-2)."<br>";
echo substr("I love web programming",0,-6)."<br>";
?>

</body>
</html>

Step 2: Save as Lab4e.php to the document root and browse it.

Step 3: Write the output and explain your observation.

Activity 4F

Activity Outcome: Use concatenate to combine two or more strings in PHP.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$string1 = 'Politeknik';

4
$string2 = 'Balik';
$string3 = 'Pulau';

$name_str = $string1 . ' ' . $string2. ' '. $string3;


echo $name_str;

?>

Step 2: Save as Lab4f.php to the document root and browse it.

Step 3: Write the output and explain your observation.

Activity 4G
Activity Outcome: Use concatenate to combine using two Variables in PHP

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$var_1 = "Jom Masuk, ";


$var_2 = "Politeknik Balik Pulau";
$JOIN_VAR = $var_1." ".$var_2." ";
echo $JOIN_VAR;

?>

Step 2: Save as Lab4g.php to the document root and browse it.

Step 3: Write the output and explain your observation.

5
Activity 4H
Activity Outcome: Use indexed array in PHP statement.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

//creates an indexed array named and assigns elements to it


$subject = array("Web programming", "Database", "Design");
//prints a text containing the array values
echo "I like study ".$subject[0].", ".$subject[1]." And
".$subject[2].".";

?>

Step 2: Save as Lab4h.php to the document root and browse it.

Step 3: Write the output and explain your observation.

Activity 4I
Activity Outcome: Use associative array in PHP statement.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$credit= array("Web"=>"3", "Database"=>"4", "Project"=>"6");

echo "Credit hour for Web is " . $credit['Web'] . " hours.";


echo "<br>";
echo "Credit hour for Database is " .$credit['Database']. "
hours.";
echo "<br>";
echo "Credit hour for Project is " .$credit['Project']. "
hours.";
?>

Step 2: Save as Lab4i.php to the document root and browse it.

6
Step 3: Write the output and explain your observation.

Activity 4J
Activity Outcome: Use multidimensional array in PHP statement.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$credit = array
(array("Web",3),array("Database",4),array("Project",6));

echo $credit[0][0].": Credit Hours= ".$credit[0][1].".<br>";


echo $credit[1][0].": Credit Hours= ".$credit[1][1].".<br>";
echo $credit[2][0].": Credit Hours= ".$credit[2][1].".<br>";
?>

Step 2: Save as Lab4j.php to the document root and browse it.

Step 3: Write the output and explain your observation.

7
Activity 4K
Activity Outcome: Use for loops and function in array statement.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$subject[0] = "Web";
$subject[1] = "Database";
$subject[2] = "Project";

for ($x = 0; $x<count($subject); $x++)


{
echo $subject[$x], "<br>" ;
}
?>

Step 2: Save as Lab4k.php to the document root and browse it.

Step 3: Write the output and explain your observation.

Activity 4L
Activity Outcome: Use foreach loops and function in array statement.

Procedure:

Step 1: Open a PHP Editor and type the following code:

<?php

$subject = array("Web"=>"3", "Database"=>"4", "Project"=>"6");

foreach($subject as $x => $val)


{
echo "$x = $val<br>";
}

?>

8
Step 2: Save as Lab4l.php to the document root and browse it.

Step 3: Write the output and explain your observation.

You might also like