Ch1,Ch2 Answers(PHP)
Ch1,Ch2 Answers(PHP)
1. A variable in PHP is a container used to store data, such as numbers, text, or arrays.
<?php
$name ="John"; // String variable
$age = 25; // Integer variable The output of example
$price = 19.99; // Float variable
Name: John
echo "Name: " . $name . “<br>";
echo "Age: " . $age . “<br>"; Age: 25
echo "Price: $" . $price;
Price: $19.99
?>
2. The echo statement is the most common
way to output text in PHP.
- The print statement works similarly to echo, but it can only output one argument
and always returns 1, making it slightly slower than echo.
3. Using the define() function (Traditional way)
- Using the const keyword (Newer way, since PHP 5.3)
4. The Main data types in PHP are:
A string is a sequence of letters, numbers, special characters and arithmetic values
or combination of all.
An integer is a whole number (positive or negative) without a decimal point.
Afloat (also called a double) is a number with a decimal point.
A Boolean represents true (true) or false (false).
5. the ++ operator is the increment operator, which increases a variable's value by
1. It can be used in two ways:
- Pre-Increment (++$var) and Post-Increment ($var++).
6. <?php
$age = 25;
echo "My age is " . $age;
?>
7. <?php
$num1 = 10;
$num2 = 5;
$addition = $num1 + $num2;
$subtraction = $num1 - $num2;
$multiplication = $num1 * $num2;
$division = $num1 / $num2;