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

  1. is_array(): Checks if a variable is an array.

      is_array($var);
      
  2. is_bool(): Checks if a variable is a boolean.

      is_bool($var);
      
  3. is_callable(): Checks if a variable is a valid callable function.

      is_callable($var);
      
  4. is_double() (alias of is_float()): Checks if a variable is a float.

      is_double($var);
      
  5. is_float(): Checks if a variable is a float.

      is_float($var);
      
  6. is_int() (alias of is_integer() and is_long()): Checks if a variable is an integer.

      is_int($var);
      
  7. is_integer() (alias of is_int() and is_long()): Checks if a variable is an integer.

      is_integer($var);
      
  8. is_long() (alias of is_int() and is_integer()): Checks if a variable is an integer.

      is_long($var);
      
  9. is_null(): Checks if a variable is null.

      is_null($var);
      
  10. is_numeric(): Checks if a variable is a number or a numeric string.

      is_numeric($var);
      
  11. is_object(): Checks if a variable is an object.

      is_object($var);
      
  12. is_real() (alias of is_float()): Checks if a variable is a float.

      is_real($var);
      
  13. is_resource(): Checks if a variable is a resource.

      is_resource($var);
      
  14. is_scalar(): Checks if a variable is a scalar (integer, float, string, or boolean).

      is_scalar($var);
      
  15. is_string(): Checks if a variable is a string.

      is_string($var);
      
  16. is_iterable(): Checks if a variable is iterable (array or object implementing Traversable interface).

      is_iterable($var);
      
  17. 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.