Lab05-PHP01 V1.01 - updated - THIS ONE
Lab05-PHP01 V1.01 - updated - THIS ONE
Php
Variable Names
Start with a dollar sign ($) followed by a letter or underscore, followed by any number of
letters, numbers, or underscores
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
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
$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
2. You should get the output “One Two Three Six”. Screenshot the output. Explain why
four and five is not printed?
If
2. Change the condition of the previous code to print “wrong answer”. Screenshot your
code and the output
Loops – For
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.
2. Concatenate your name into the “Hello world”. Screenshot the output.
Optional: Ternary Operator
$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
echo "\n";
$out = "Hello";
$out = $out . " ";
$out .= "World!";
$out .= "\n";
echo $out;
$count = 0;
$count += 1;
echo "Count: $count\n";
Casting
$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