0% found this document useful (0 votes)
10 views

Lab05-PHP01 V1.01 - updated - THIS ONE

Uploaded by

Chloe Tee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab05-PHP01 V1.01 - updated - THIS ONE

Uploaded by

Chloe Tee
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

LAB 05

Php

Variable Names
Start with a dollar sign ($) followed by a letter or underscore, followed by any number of
letters, numbers, or underscores

1. Type the following code, and save it as .php in C:\xampp\htdocs


<?php
$abc = 12;
$total = 0;
$largest_so_far = 0;
echo ($abc);
echo ($total);
echo ($largest_so_far);
?>

2. Run the code from your browser, you should be able to create the variable
successfully without error.

3. For each of the variable below, try to include the variable one by one into your
previous codes. Run the code from the browser, and you should observe “Parse
error ….”

abcd = 12;
$2php = 0;
$bad-punc = 0;

4. For each of the variable which display parse error, describe the error and suggest a
rectification.
Variable Name in Expression

1. Try the following code. Screenshot the output. Does the following code works
without error?
$x = 2;
$y = x + 5;
print $y;

2. Try the following code. Screenshot the output. Does the following code works
without error?
$x = 2;
y = $x + 5;
print $x;
Adding Two Variables in an Expression

1. Try out the following code.


<?php
$x = "15" + 27;
echo($x);
echo("\n");
?>

2. Observe and screenshot the output. Notice that the first number is a string, php will
automatically convert the string to an integer.
Increment / Decrement Operator

1. Try out both of the following code.

$x = 12;
$y = 15 + $x++;
echo "x is $x and y is $y \n";

$x = 12;
$y = 15 + $x;
$x = $x + 1;
echo "x is $x and y is $y \n";

2. Observe the results produced by both code. Is the output same? Which of the code is
much more preferred?
Equality

1. Try out the following code, and try to understand.


<?php
$first = 10;
$second = 10.0;
$third = "10";

if ($first == 10) print "One";


if ($second == 10) print "Two";
if ($third == 10) print "Three";

if ($third === 10) print "Four";


if ($second === 10) print "Five";
if ($first === 10) print "Six";
?>

2. You should get the output “One Two Three Six”. Screenshot the output. Explain why
four and five is not printed?
If

1. Try the following if statement.


<?php
$ans = 42;
if ( $ans == 42 ) {
print "Hello world!\n";
} else {
print "Wrong answer\n";
}
?>

2. Change the condition of the previous code to print “wrong answer”. Screenshot your
code and the output
Loops – For

1. Try the following code.

for($count=1; $count<=6; $count++ ) {


echo "$count times 6 is " . $count * 6;
echo "\n";
}

2. Change the condition of the code, screenshot the output.


- Count starts from 10
- Print the value from 10 to 20

3. Write a while loop to produce the same output as the previous question.
- Count starts from 10
- Print the value from 10 to 20
Optional Understanding Single Quotes and Double Quotes

1. Double Quote
<?php
header('Content-type: text/plain');
echo "this is a simple string\n";
echo "You can also have embedded newlines in
strings this way as it is
okay to do";
// Outputs: This will expand:
// a newline
echo "This will expand: \na newline";
// Outputs: Variables do 12
$expand = 12;
echo "Variables do $expand\n";
?>

2. Single Quote
<?php
header('Content-type: text/plain');
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
3. In your own words, explain the difference between single quote and double quote.
Optional: String Concatenation
PHP uses the period character for concatenation because the plus character would instruct
PHP to do the best it could to add the two things together, converting if necessary.

1. Basic string concatenation.


$a = 'Hello ' . 'World!';
echo $a . "\n";

2. Concatenate your name into the “Hello world”. Screenshot the output.
Optional: Ternary Operator

1. Try out the following code and understand it.

$www = 123;
$msg = $www > 100 ? "Large" : "Small" ;
echo "First: $msg \n";
$msg = ( $www % 2 == 0 ) ? "Even" : "Odd";
echo "Second: $msg \n";
$msg = ( $www % 2 ) ? "Odd" : "Even";
echo "Third: $msg \n";

2. Task, change the “if else” statement to a single line code using ternary operator.
Screenshot the output
$value = 15;
$msg = "";
if ($value < 15){
$msg = "value smaller than 15";
}
else{
$msg = "value greater than 15";
}
echo ($msg) . "\n\n";
Optional: Side-Effect Assignment

1. Try out the following code.

echo "\n";
$out = "Hello";
$out = $out . " ";
$out .= "World!";
$out .= "\n";
echo $out;
$count = 0;
$count += 1;
echo "Count: $count\n";

Casting

1. Try out the following code.

$a = 56; $b = 12;
$c = $a / $b;
echo "C: $c\n";
$d = "100" + 36.25 + TRUE;
echo "D: ". $d . "\n";
echo "D2: ". (string) $d . "\n";
$e = (int) 9.9 - 1;
echo "E: $e\n";
$f = "sam" + 25;
echo "F: $f\n";
$g = "sam" . 25;
echo "G: $g\n";

Note: "sam" + 25 will give you error, because you cannot use "+" operator for String and int

You might also like