PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

1. String

A string is a sequence of characters enclosed in quotes. PHP supports both single-quoted and double-quoted strings.

  • Single-quoted strings: These are treated almost literally. Variables and escape sequences (except for \\ and \') are not interpreted.
				
					$name = 'John';
echo 'Hello, $name'; // Output: Hello, $name

				
			
  • Double-quoted strings: These allow for variable interpolation and escape sequences.
				
					$name = 'John';
echo "Hello, $name"; // Output: Hello, John

				
			

2. Integer

An integer is a whole number without a decimal point. It can be positive or negative.

  • Decimal (base 10):
				
					$int = 42;

				
			
  • Hexadecimal (base 16):
				
					$hex = 0x2A; // 42 in decimal

				
			
  • Octal (base 8):
				
					$octal = 052; // 42 in decimal

				
			

3. Float

A float (or double) is a number with a decimal point or a number in exponential form.

				
					$float = 3.14;
$floatExp = 2.5e3; // 2500

				
			

4. Boolean

A boolean represents two possible states: ‘TRUE‘ or’FALSE‘.

				
					$isTrue = true;
$isFalse = false;

				
			

5. Array

An array is a collection of values. In PHP, arrays are actually ordered maps, meaning they associate values to keys.

  • Indexed arrays:
				
					$array = array(1, 2, 3);
echo $array[0]; // Output: 1

				
			
  • Associative arrays:
				
					$assocArray = array("name" => "John", "age" => 30);
echo $assocArray["name"]; // Output: John

				
			
  • Multidimensional arrays:
				
					$multiArray = array(
    array(1, 2, 3),
    array(4, 5, 6),
    array(7, 8, 9)
);
echo $multiArray[1][2]; // Output: 6

				
			

6. Object

An object is an instance of a class. Objects allow you to model real-world entities with properties and methods.

				
					class Person {
    public $name;
    public $age;

    function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    function greet() {
        return "Hello, my name is " . $this->name;
    }
}

$person = new Person("John", 30);
echo $person->greet(); // Output: Hello, my name is John

				
			

7. NULL

NULL is a special type that only has one value: NULL. It represents a variable with no value.

				
					$var = NULL;
if (is_null($var)) {
    echo "The variable is NULL";
}

				
			

Type Juggling and Type Casting

PHP is a loosely typed language, which means it can automatically convert between different types based on the context (type juggling). However, you can also explicitly cast variables.

  • Automatic type juggling:
				
					$sum = "3" + 4; // $sum is 7 (integer)

				
			
  • Explicit type casting:
				
					$num = (int) "42"; // $num is 42 (integer)
$str = (string) 42; // $str is "42" (string)

				
			

Understanding these data types and their behaviors is essential for effective PHP programming. Each type comes with its own set of functions and methods that can be used to manipulate and handle data efficiently.

Scroll to Top