Professional Documents
Culture Documents
Lab Practice 2
Lab Practice 2
Lab practice 1
1. Case Sensitivity
Write a PHP code to show that these three lines are equivalent:
echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");
On the other hand, variables, are case-sensitive
That is, $name, $NAME, and $NaME are three different variables
2. Statements and Semicolons
Find the error in the following PHP code
<?php
if ($a == $b) {
echo "Rhyme? And Reason?"
}
echo "Hello, world"
?>
3. Comments
4. If you run this piece of PHP code, what is the output?
4.1.<?php $d = 4; # Set $d to 4. ?> Then another <?php echo $d; ?>
4.2.<H1>Standard tag: <?php /*echo " standard tag"; */?>multi line c style
comment</H1>
4.3.
<?php
$l = 12;
$m = 13;
• $i = 9;
• /*
1
• $j = 10; /* This is a comment */
• $k = 11;
• Here is some comment text.
• */
4.5.
<?php
/*
$f->setPattern('/^\d.*/');
*/
?>
4.6.what is the output?
$name = "Guido";
echo "Hi, $name <br/>";
echo 'Hi, $name';
4.7.what is the output?
<?php
$name = "Paul"; // declaration and initialization
print( ‘Welcome to PHP, $name!’ );
?
4.8.what is the output?
$dosPath = 'C:\\WINDOWS\\SYSTEM';
$publisher = 'Tim O\'Reilly';
echo "$dosPath $publisher";
2
<html>
<?php
?>
<!-- end PHP script -->
<head>
<meta charset = "utf-8">
<title>Simple PHP document</title>
</head>
<body>
<!-- print variable name’s value -->
<h1></h1>
</body>
</html>
sort($person);
asort($creator);
4.12. arsort($creator);
4.13. ksort ($creator);
5. Converting Between Data Types
3
Converting between different data types may be necessary when performing arithmetic operations
with variables.
Figure 19.3 demonstrates type conversion of some types introduced in Fig. 19.2.
<!DOCTYPE html>
<html>
<head>
p { margin: 0; }
</style>
</head>
<body>
<?php
$testDouble = 79.2;
$testInteger = 12;
<?php
4
print( "<p>$testString is a(n) " . gettype( $testString )
. "</p>" );
. "</p>" );
. "</p>" );
<?php
$testString</p>" );
5
</body>
</html>
1) Do Variables in PHP have types? If the answer is yes, how are variables are typed? Give ex-
ample.
5) Calling function settype can result in loss of data. Explain with example.
6) Unlike settype, casting does not change a variable’s content—it creates a temporary copy of
a variable’s value in memory. Casting is useful when a different type is required in a specific
operation but you would like to retain the variable’s original value and type. Write the lines
that show that the type and value of $data remain unchanged even after it has been cast.
6.