PHP Tags
PHP code can be embedded in HTML using special tags. The most common tag is:
<?php
// PHP code goes here
?>
Another shorthand tag (if enabled on your server) is:
<?
// PHP code goes here
?>
And for outputting a variable:
<?= $variable ?>
PHP Statements
PHP statements end with a semicolon (;
). It’s crucial to include it at the end of each statement to avoid syntax errors. For example:
<?php
echo "Hello, World!";
?>
Comments
Comments in PHP can be single-line or multi-line. Single-line comments use //
or #
, while multi-line comments are enclosed in /* */
.
<?php
// This is a single-line comment
# This is another single-line comment
/*
This is a multi-line comment
spanning multiple lines.
*/
?>
Case Sensitivity
PHP is case-sensitive in terms of variables but not for keywords, function names, and classes. For instance, $Variable
and $variable
are different, but echo
and ECHO
are equivalent.
Data Types in PHP
PHP supports several data types, including:
- Integer
- Float
- String
- Boolean
- Array
- Object
- NULL
- Resource
Let’s explore each of these in detail.
1. Integer
An integer is a whole number, positive or negative, without decimals. Integers can be written in decimal, hexadecimal, or octal notation.
<?php
$decimal = 123; // decimal number
$negative = -123; // negative number
$hex = 0x1A; // hexadecimal number
$octal = 0123; // octal number
?>
2. Float
Floats, also known as floating-point numbers, are numbers with a decimal point or in exponential form.
<?php
$float = 3.14; // floating-point number
$scientific = 1.2e3; // scientific notation
$negativeFloat = -2.7;
?>
3. String
A string is a sequence of characters, enclosed within single ('
) or double ("
) quotes.
<?php
$single = 'Hello, World!';
$double = "Hello, World!";
String Concatenation
You can concatenate strings using the .
operator.
<?php
$firstName = "John";
$lastName = "Doe";
$fullName = $firstName . " " . $lastName;
echo $fullName; // Outputs "John Doe"
?>
String Interpolation
When using double quotes, you can include variables directly within the string.
<?php
$name = "John";
echo "Hello, $name!"; // Outputs "Hello, John!"
?>
4. Boolean
A boolean represents two possible states: TRUE
or FALSE
.
<?php
$isTrue = true;
$isFalse = false;
?>
Booleans are often used in conditional statements to control the flow of a program.
5. Array
An array is a collection of values, which can be of different data types. PHP supports both indexed and associative arrays.
Indexed Array
In an indexed array, elements are accessed using numeric indices.
<?php
$numbers = array(1, 2, 3, 4, 5);
echo $numbers[0]; // Outputs 1
?>
Associative Array
In an associative array, elements are accessed using named keys.
<?php
$ages = array("John" => 25, "Jane" => 30, "Doe" => 35);
echo $ages["Jane"]; // Outputs 30
?>
Multidimensional Array
Arrays can contain other arrays, forming multidimensional arrays.
<?php
$people = array(
"John" => array("age" => 25, "gender" => "male"),
"Jane" => array("age" => 30, "gender" => "female")
);
echo $people["John"]["age"]; // Outputs 25
?>
6. Object
Objects are instances of classes, which are blueprints for objects. A class can contain properties and methods.
Defining a Class and Creating an Object
<?php
class Car {
public $color;
public $model;
public function __construct($color, $model) {
$this->color = $color;
$this->model = $model;
}
public function message() {
return "My car is a " . $this->color . " " . $this->model . ".";
}
}
$myCar = new Car("red", "Toyota");
echo $myCar->message(); // Outputs "My car is a red Toyota."
?>
7. NULL
NULL represents a variable with no value. It can be assigned using the NULL
constant.
<?php
$var = NULL;
?>
8. Resource
A resource is a special variable holding a reference to an external resource, such as a file, database connection, or stream.
<?php
$file = fopen("test.txt", "r");
// $file is a resource variable
?>
Conclusion
PHP’s versatility and ease of use make it an excellent choice for web development. Understanding the basic syntax and data types is the first step in becoming proficient in PHP. Whether you’re creating simple scripts or complex web applications, mastering these fundamentals will provide a solid foundation for further exploration and development in PHP.
As you progress, you’ll encounter more advanced features like classes and objects, error handling, and more sophisticated data structures. But for now, with this basic knowledge, you’re well on your way to becoming a PHP developer!