Basic PHP Syntax
PHP Tags
PHP code is embedded within HTML using PHP tags. The most common way to open and close PHP code is:
<?php
// PHP code goes here
?>
There’s also a shorthand syntax for short echo statements:
<?= "Hello, World!"; ?>
However, using <?php ... ?>
is the recommended approach to ensure compatibility across different environments.
Echo and Print Statements
echo
and print
are used to output data to the browser. Both are language constructs, not functions, so parentheses are not required, though they can be used.
- Echo:
echo "Hello, World!";
echo can output multiple strings separated by commas:
echo "Hello", ", ", "World!";
- Print:
print "Hello, World!";
print
can only output one string at a time and always returns 1
, making it slightly slower than echo
.
Comments
Comments are used to annotate code or temporarily disable code from executing.
- Single-line comments:
// This is a single-line comment
# This is also a single-line comment
- Multi-line comments:
/*
This is a multi-line comment
that spans multiple lines
*/
Variables and Data Types
Variables in PHP are prefixed with a dollar sign ($
) and can hold different data types. PHP is a loosely typed language, meaning variables can change type dynamically.
- Declaring Variables:
$variableName = "Hello, World!";
-
Data Types:
- String: Sequence of characters.
$string = "This is a string";
- Integer: Whole numbers.
$integer = 42;
- Float (Double): Numbers with a decimal point.
$float = 3.14;
- Boolean: true or false.
$boolean = true;
- Array: Collection of values.
$array = array("apple", "banana", "cherry");
- Object: Instances of classes.
class MyClass { // properties and methods } $object = new MyClass();
- NULL: Special type for a variable with no value.
$nullVar = NULL;
-
Type Checking Functions
-
is_array()
: Checks if a variable is an array.is_array($var);
-
is_bool()
: Checks if a variable is a boolean.is_bool($var);
-
is_callable()
: Checks if a variable is a valid callable function.is_callable($var);
-
is_double()
(alias ofis_float()
): Checks if a variable is a float.is_double($var);
-
is_float()
: Checks if a variable is a float.is_float($var);
-
is_int()
(alias ofis_integer()
andis_long()
): Checks if a variable is an integer.is_int($var);
-
is_integer()
(alias ofis_int()
andis_long()
): Checks if a variable is an integer.is_integer($var);
-
is_long()
(alias ofis_int()
andis_integer()
): Checks if a variable is an integer.is_long($var);
-
is_null()
: Checks if a variable is null.is_null($var);
-
is_numeric()
: Checks if a variable is a number or a numeric string.is_numeric($var);
-
is_object()
: Checks if a variable is an object.is_object($var);
-
is_real()
(alias ofis_float()
): Checks if a variable is a float.is_real($var);
-
is_resource()
: Checks if a variable is a resource.is_resource($var);
-
is_scalar()
: Checks if a variable is a scalar (integer, float, string, or boolean).is_scalar($var);
-
is_string()
: Checks if a variable is a string.is_string($var);
-
is_iterable()
: Checks if a variable is iterable (array or object implementing Traversable interface).is_iterable($var);
-
is_countable()
: Checks if a variable is countable (array or object implementing Countable interface).is_countable($var);
Constants
Constants are like variables, but once they are defined, they cannot be changed. Constants do not use a $
prefix and are defined using the define()
function.
- Defining a Constant:
define("CONSTANT_NAME", "Value of the constant");
- Using a Constant:
echo CONSTANT_NAME;
Constants are often used for values that remain constant throughout the execution of the script, like database credentials or configuration settings.