Class 2: PHP Basics - Syntax and Data Types
Assignment 1: Display a Simple Message
Code:
<?php echo "Hello, World!"; ?>
|
Explanation: This script demonstrates the use of echo to output a simple message.
Assignment 2: Use print to Display a Message
Code:
print "Welcome to PHP Basics!"; ?>
|
Explanation: This script demonstrates the use of print to output a message, similar to echo.
Assignment 3: Declare and Display Variables
Code:
<?php $greeting = "Hello"; $year = 2024; ?>
|
Explanation: This script shows how to declare variables and concatenate them with a string using ..
Assignment 4: Basic Data Types - Strings
Code:
Explanation: This script demonstrates how to work with string data types.
<?php $name = "Alice"; $occupation = "Engineer"; echo "Name: $name\n"; echo "Occupation: $occupation\n"; ?>
|
Assignment 5: Basic Data Types - Integers
Code:
Explanation: This script shows how to work with integer data types.
<?php $age = 30; $yearOfBirth = 1994; echo "Year of Birth:
$yearOfBirth\n"; ?>
|
Assignment 6: Basic Data Types - Floats
Code:
Explanation: This script demonstrates the use of floating-point numbers.
<?php $price = 19.99; $discount = 0.15; echo "Price: $$price\n"; echo "Discount: $discount\n"; ?>
|
Assignment 7: Basic Data Types - Booleans
Code:
Explanation: This script shows how to use boolean values and conditional statements to display them.
<?php $isAvailable = true; $isSoldOut = false; echo "Available: " . ($isAvailable ? "Yes" : "No") . "\n"; echo "Sold Out: " . ($isSoldOut ?
"Yes" : "No") . "\n"; ?>
|
Assignment 8: Type Casting - String to Integer
Code:
<?php $stringNumber
= "123"; $integerNumber
= (int)$stringNumber; echo
"String: $stringNumber\n"; echo
"Integer: $integerNumber\n"; ?> |
Explanation: This script demonstrates type casting from a string to an integer.
Assignment 9: Type Casting - Integer to Float
Code:
<?php $integerNumber
= 45; $floatNumber
= (float)$integerNumber; echo "Integer: $integerNumber\n"; echo
"Float: $floatNumber\n"; ?> |
Explanation: This script shows type casting from an integer to a float.
Assignment 10: Type Casting - Float to String
Code:
<?php $floatNumber
= 9.99; $stringNumber
= (string)$floatNumber; echo "Float: $floatNumber\n"; echo
"String: $stringNumber\n"; ?> |
Explanation: This script demonstrates type casting from a float to a string.
Assignment 11: Concatenate Strings and Variables
Code:
<?php $firstName =
"John"; $lastName =
"Doe";
echo
"Full Name: " . $firstName . " " . $lastName; ?> |
Explanation: This script demonstrates concatenating strings with variables.
Assignment 12: Arithmetic Operations with Variables
Code:
<?php $x = 10; $y = 5; $sum = $x + $y; $product =
$x * $y; echo "Sum: $sum\n"; echo
"Product: $product\n"; ?> |
Explanation: This script performs basic arithmetic operations using variables.
Assignment 13: Use of Constants
Code:
<?php define("PI",
3.14159); echo "Value of PI: " . PI; ?> |
Explanation: This script demonstrates how to define and use a constant.
Assignment 14: Check Variable Types
Code:
<?php $variable =
10; echo "Type of variable: " . gettype($variable) . "\n"; $variable = "Hello"; echo "Type of variable: " . gettype($variable) . "\n"; ?> |
Explanation: This script checks and displays the type of a variable using gettype().
Assignment 15: Arithmetic Operations with Floats
Code:
<?php $price =
19.99; $tax = 0.07; $total = $price + ($price * $tax); echo "Total Price: $" . number_format($total, 2); ?> |
Explanation: This script performs arithmetic operations with floats and formats the result.
Assignment 16: Variable Scope
Code:
<?php function
testScope() {
$localVar = "I'm local";
echo $localVar . "\n"; } testScope(); // echo
$localVar; // This will cause an error ?> |
Explanation: This script demonstrates variable scope by showing that variables inside a function are local to that function.
Assignment 17: Using isset() to Check Variable Initialization
Code:
<?php $var; if (isset($var)) {
echo "Variable is set."; } else {
echo "Variable is not set."; } ?> |
Explanation: This script checks whether a variable is set using isset().
Assignment 18: Type Casting - Boolean to Integer
Code:
<?php $booleanValue
= true; $integerValue
= (int)$booleanValue; echo "Boolean: $booleanValue\n"; echo
"Integer: $integerValue\n"; ?> |
Explanation: This script demonstrates type casting from a boolean to an integer.
Assignment 19: Concatenate String and Integer
Code:
<?php $age = 25; $message =
"Age: " . $age; echo $message; ?> |
Explanation: This script concatenates a string with an integer and displays the result.
Assignment 20: Using var_dump() to Display Variable Types and Values
Code:
<?php $integer =
42; $float =
3.14; $string =
"Hello"; var_dump($integer); var_dump($float); var_dump($string); ?> |
Explanation: This script uses var_dump() to display the type and value of different variables.
These assignments cover a variety of fundamental topics related to PHP syntax and data types, providing students with hands-on practice in declaring variables, type casting, and using PHP’s basic features.