About Lesson
How do you write PHP code?
PHP code is written between <?php
and ?>
tags. Example:
PHP
<?php
echo "PHP code runs here";
?>
What are PHP statements?
PHP statements are executed one by one. Each PHP statement ends with a semicolon (;
). Example:
PHP
<?php
echo "Hello";
echo "World";
?>
What are PHP variables?
Variables in PHP are used to store data. A variable starts with the $
symbol followed by the name of the variable. Rules:
- Start with
$
- Can’t start with a number
- Are case-sensitive
Example:
PHP
<?php
$name = "John";
$age = 25;
echo $name;
?>
What are the types of variables in PHP?
- String: Text
- Integer: Whole numbers
- Float (Double): Decimal numbers
- Boolean:
true
orfalse
- Array: Multiple values
- Object: Instance of a class
- NULL: No value
Example:
PHP
<?php
$text = "Hello"; // String
$num = 123; // Integer
$price = 45.67; // Float
$is_valid = true; // Boolean
$colors = array("Red", "Blue"); // Array
$x = null; // NULL
?>
How to display variables in PHP?
Use echo
or print
to display variables. Example:
PHP
<?php
$name = "Alice";
echo "My name is " . $name;
?>