There are many similarities between Python and PHP, but there significant differences:
- PHP variables start with $ signs
- PHP statements must end with a semi-colon
- Instead of indentation, PHP uses pairs of curly brackets { } to enclose blocks of code inside conditional statements and loops.
- Conditions are enclosed in parentheses ( ).
Case Sensitivity
- PHP keywords are not case sensitive (ECHO = echo = Echo)
- PHP variables are case sensitive ($counter != $Counter)
Comments
1 2 3 | # A single line comment ''' A multi line comment ''' |
1 2 3 4 | // A single line comment # Another single line comment /* A multi line comment */ |
Printing
1 | print (f "Counter = {counter}" ) |
1 2 | echo "Counter = " . $counter ; /* using concatenation */ echo "Counter = $counter" ; /* simple variables inside string */ |
Assignment
01 02 03 04 05 06 07 08 09 10 11 12 13 | counter = 0 password = "xyz" password = 'xyz' MAXSTUDENTS = 20 colours = [ "red" , "blue" , "green" ] computerIDs = [""] * 10 computerId[ 0 ] = "AHS-210012" fruits = [] fruits.append( "Apple" ) |
01 02 03 04 05 06 07 08 09 10 11 12 | $counter = 0; $password = "xyz" ; define( "MAXSTUDENTS" , 20); $colours = array ( "red" , "blue" , "green" ); $computerIDs = array_fill (0, 10, "" ); $computerId [0] = "AHS-210012" ; $fruits = array (); array_push ( $fruits , "Apple" ); |
Operators
Arithmetic operators + – * / ** (exponentiation) % (modulus) are same in Python and PHP
1 2 3 4 | total = total + number total + = number fullname = forename + " " + surname |
1 2 3 4 | $total = $total + $number ; $total += $number ; $fullname = $forename . " " . $surname ; |
Predefined Functions
1 2 3 | nameLength = len (fullname) dice = random.randint( 1 , 6 ) noOfFruits = len (fruits) |
1 2 3 | $nameLength = strlen ( $fullname ); $dice = rand(1,6); $noOfFruits = count ( $fruits ); |
Conditional Statements
1 2 3 4 5 6 | if guess = = number: print ( "You guessed the number" ) elif guess < number: print ( "Your guess is too small" ) else print ( "Your guess is too big" ) |
1 2 3 4 5 6 7 | if ( $guess == $number ) { echo "You guessed the number" ; } elseif ( $guess < $number ) { echo "Your guess is too small" ; } else { echo "Your guess is too big" ; } |
Switch Statements
Python does not have a switch statement. The simplest equivalent is an if..elif..elif..else statement.
01 02 03 04 05 06 07 08 09 10 11 12 13 | switch ( $favcolor ) { case "red" : echo "Your favourite colour is red!" ; break ; case "blue" : echo "Your favourite colour is blue!" ; break ; case "green" : echo "Your favourite colour is green!" ; break ; default : echo "Your favourite colour is neither red, blue, nor green!" ; } |
Fixed Loops (by index)
1 2 | for x in range ( 10 ): print (x) |
1 2 3 | for ( $x = 0; $x < 10; $x ++) { echo "<p>$x</p>\n" ; } |
Fixed Loops (by item)
1 2 3 | colours = [ "red" , "blue" , "green" ] for colour in colours: print (colour) |
1 2 3 4 | colours = array ( "red" , "blue" , "green" ); foreach ( $colours as $colour ) { echo "<p>$colour</p>\n" ; } |