WikiGalaxy

Personalize

PHP Syntax Overview

Introduction to PHP Syntax:

PHP is a server-side scripting language designed primarily for web development. It is embedded within HTML and is executed on the server.

Basic PHP Script:

A simple PHP script starts with <?php and ends with ?>. This tells the server to process the code within these tags.


      <?php
      echo "Hello, World!";
      ?>
    

Variables in PHP:

Variables in PHP start with a dollar sign ($) followed by the name of the variable. Variable names are case-sensitive.


      <?php
      $greeting = "Hello, World!";
      echo $greeting;
      ?>
    

PHP Data Types:

PHP supports several data types such as strings, integers, floats, booleans, arrays, objects, NULL, and resources.


      <?php
      $string = "Hello";
      $integer = 42;
      $float = 3.14;
      $boolean = true;
      ?>
    

PHP Arrays:

Arrays in PHP are used to store multiple values in a single variable. There are three types of arrays: indexed, associative, and multidimensional.


      <?php
      $colors = array("red", "green", "blue");
      echo $colors[0]; // Outputs: red
      ?>
    

Control Structures:

PHP supports standard control structures like if, else, switch, while, for, and foreach loops.


      <?php
      $number = 5;
      if ($number > 0) {
          echo "Positive number";
      }
      ?>
    

Functions in PHP:

Functions are reusable pieces of code that perform a specific task. They are defined using the function keyword.


      <?php
      function greet($name) {
          return "Hello, " . $name;
      }
      echo greet("Alice");
      ?>
    

PHP Comments:

Comments in PHP can be single-line or multi-line. Single-line comments use // or #, while multi-line comments are enclosed within /* */.


      <?php
      // This is a single-line comment
      # Another single-line comment
      /* This is a 
         multi-line comment */
      ?>
    
logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025