You are on page 1of 6

PHP

Lab practice 1

Date: 03 April 2023

Titles of the Practice:

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;

/* A comment begins here


?>

<p>Some stuff you want to be HTML.</p>

<?= $n = 14; ?>


*/
echo("l=$l m=$m n=$n\n");
?><p>Now <b>this</b> is regular HTML...</p>
4.4.

• $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";

4.9.what is the output?

$person = array("Edison", "Wankel", "Crapper");

$creator = array('Light bulb' => "Edison",

'Rotary Engine' => "Wankel",


'Toilet' => "Crapper");
foreach ($person as $name) {
echo ‘Hello, {$name}<br/>’;
}
foreach ($creator as $invention => $inventor) {
echo ‘{$inventor} invented the {$invention}<br/>’;
}

With single quote and double quote


<!DOCTYPE html>

<!-- Fig. : first.php -->


<!-- Simple PHP program. -->

2
<html>
<?php

$person = array("Edison", "Wankel", "Crapper");

$creator = array('Light bulb' => "Edison",


'Rotary Engine' => "Wankel",
'Toilet' => "Crapper");
// declaration and initialization

foreach ($person as $name) {


echo 'Hello, {$name}<br/>';
}
foreach ($creator as $invention => $inventor) {
echo '{$inventor} invented the {$invention}<br/>';
}

?>
<!-- end PHP script -->
<head>
<meta charset = "utf-8">
<title>Simple PHP document</title>
</head>
<body>
<!-- print variable name’s value -->
<h1></h1>
</body>
</html>

4.10. what is the output?

Note that originally, $person = array("Edison", "Wankel", "Crapper");

sort($person);

4.11. what is the output?

Note that originally, $creator = array('Light bulb' => "Edison",

'Rotary Engine' => "Wankel",

'Toilet' => "Crapper");

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.

Type conversions can be performed using function settype.

Figure 19.3 demonstrates type conversion of some types introduced in Fig. 19.2.

<!DOCTYPE html>

<!-- Fig. 19.3: data.php -->

<!-- Data type conversion. -->

<html>

<head>

<meta charset = "utf-8">

<title>Data type conversion</title>

<style type = "text/css">

p { margin: 0; }

.head { margin-top: 10px; font-weight: bold; }

.space { margin-top: 10px; }

</style>

</head>

<body>

<?php

// declare a string, double and integer

$testString = "3.5 seconds";

$testDouble = 79.2;

$testInteger = 12;

?><!-- end PHP script -->

<!-- print each variable’s value and type -->

<p class = "head">Original values:</p>

<?php

4
print( "<p>$testString is a(n) " . gettype( $testString )

. "</p>" );

print( "<p>$testDouble is a(n) " . gettype( $testDouble )

. "</p>" );

print( "<p>$testInteger is a(n) " . gettype( $testInteger )

. "</p>" );

?><!-- end PHP script -->

<p class = "head">Converting to other data types:</p>

<?php

// call function settype to convert variable

// testString to different data types

print( "<p>$testString " );

settype( $testString, "double" );

print( " as a double is $testString</p>" );

print( "<p>$testString " );

settype( $testString, "integer" );

print( " as an integer is $testString</p>" );

settype( $testString, "string" );

print( "<p class = 'space'>Converting back to a string results in

$testString</p>" );

// use type casting to cast variables to a different type

$data = "98.6 degrees";

print( "<p class = 'space'>Before casting: $data is a " .

gettype( $data ) . "</p>" );

print( "<p class = 'space'>Using type casting instead:</p>

<p>as a double: " . (double) $data . "</p>" .

"<p>as an integer: " . (integer) $data . "</p>" );

print( "<p class = 'space'>After casting: $data is a " .

gettype( $data ) . "</p>" );

?><!-- end PHP script -->

5
</body>

</html>

1) Do Variables in PHP have types? If the answer is yes, how are variables are typed? Give ex-
ample.

2) Which lines print the value of each variable?

3) Which lines print the type of each variable?

4) How is the type of variable obtained?

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.

You might also like