Syntax


A PHP script can be placed anywhere in the document. A PHP script starts with :
Example <h1>My first PHP page</h1> <?php echo "Hello World!"; ?>

Variables


In PHP, a variable starts with the $ sign, followed by the name of the variable:
Example <?php $txt1 = "Hello world!"; $x1 = 5; $y1 = 10.5; ?> Rules for Variables
       1. A variable starts with the $ sign, followed by the name of the variable

        2. A variable name must start with a letter or the underscore character

        3.  A variable name cannot start with a number

        4. A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

        5. Variable names are case-sensitive ($age and $AGE are two different variables)
            

How to print


Using

Echo and Print

Statement.

echo and print are more or less the same. They are both used to output data to the screen. The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print.

Echo


<?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?>

Print


          PHP is Fun!";
         print "Hello world!";
         print "I'm about to learn PHP!";
         print "This ", "string ", "was ", "made ", "with multiple parameters.";
         ?>
        
          

Data Variables


Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:


  • String
  • Integer
  • Float (floating point numbers - also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource
Php Objects An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared. <?php class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model; ?>
CONTENTS