Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.


Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons

LoginGet Started
← Back to Lessons
Weekly Coding Challenge

Every week, we pick a real-life project to build your portfolio and get ready for a job. All projects are built with ChatGPT as co-pilot!

Start the Challenge
Podcast: Code Sets You Free

A tech-culture podcast where you learn to fight the enemies that blocks your way to become a successful professional in tech.

Listen the podcast
Edit on Github

Understanding PHP Syntax

What is PHP?
  • Printing values

What is PHP?

PHP is one of the most used back-end languages in the web. In terms of functionality, it lets you do pretty much what any other back-end language lets you. The syntax is pretty similar to JavaScript, and its biggest strength is the community – you can find tutorials and documentation for everything you do.

PHP has proven to be a great survivor: 82% of the web uses PHP. Is the second language with the most interest from Google. In a stack-overflow 2018 developer survey, PHP was the most popular language amongst PHP, Ruby, Nodejs and Python. PHP Frameworks are phenomenal: Laravel, Phpixie, etc.

JavaScript vs PHP

PHP and JavaScript are like Apples and Oranges. In terms of functionality, they have NOTHING in common: they don’t serve the same purpose, they don’t do the same things, they come from different backgrounds, etc.

The only things that they have in common are:

  • You can do the main logical operations and control the flow the same way: Like in any other programming language, you have loops, while, if..else, switch, etc. (PHP has even more options – we’ll talk about those later).
  • **They have almost the same syntax:**You have to end every line with a semi-colon; functions are declared the same way; loops are declared the same way; logical operations are the same; etc.


There are only a few differences – here is the explanation:

In JavaScriptIn PHP
NumberInstead of one number data-type, you now have two: Integer and Float. An integer does not have decimals:
$myNumber = 23.23; //float
$myNumber = 54; //integer
$myNumber = 12.00; //float (even with 00 as decimals).
UndefinedThe undefined data-type is not available in PHP. Here undefined and null are the same data-type.
$myNumber; //is null because it was not defined
ArrayThey have both numerical index arrays and associative arrays. The difference is that JavaScript calls "Dictionaries" the PHP associative arrays.
$array = array('Juan','John','Steven'); //array of numeric indexes
$array = array('SSN-55532323' => 'Juan', 'SSN-99948334' => 'John', 'SSN-99330323' => 'Steven');
//associative array, using strings as indexes instead of integers.
StringIs the same in PHP.

Printing values

Console.log is amazing in JS, but in PHP, you will have to use echo for simple data-types, and print_r to print more complex data-types (like arrays and objects).

php syntax With JavaScript

1var simpleValue = ‘hello’; 2console.log(simpleValue); 3//This will print the content of the variable 4var arrayValue = [‘Hello’,23, 76, ‘World’,43]; 5console.log(arrayValue); 6//This will print the content of the array and its elements.

php syntax With PHP

1$simpleValue = ‘Hello’; 2echo $simpleValue; //this will print the content 3$arrayValue = array(‘Hello’,23,76,’World’,43); 4echo $arrayValue; //this will not work 5print_r($arrayValue); //this will work, printing the content of the array in a format like this: 6CopyArray 7( 8 [0] => Hello 9 [1] => 23 10 [2] => 76 11 [3] => World 12 [4] => 43 13)

Working With Arrays

PHP started as a functional-programming language and still has a lot of things that will work in functions instead of objects. That is why it is very important to review the basic array operations; the syntax may look different, but, in the end, they have the same purpose.

Looping Arrays

php syntax With JavaScript

1for(var i = 0; i<myArray.length; i++){ 2console.log(myArray[i]; 3} 4myArray.forEach(function(item,index,array) { 5console.log(item); 6});
1for($i=0; $i<count($myArray);$i++){ 2print_r($myArray[i]); 3} 4foreach($myArray as $item){ 5print_r($item); 6} 7foreach($myArray as $index => $value){ 8print_r($value); 9}

Adding and Removing Items

php syntax With JavaScript

1var myArray = [‘Academy’, ‘Coding’]; 2myArray.push(‘4Geeks’); //Adding an item 3//to remove the item in the INDEX position 4myArray.splice(index, 1);

php syntax With PHP

1$myArray = array(‘Academy’,’Coding’); 2array_push($myArray,4Geeks’); //adding an item 3//to remove the item in the index position 4unset($myArray[index]); 5$myArray = array_values($myArray);

Sorting Functions for Arrays

php syntax With JavaScript

1const myArray = [2,5,1,4,7]; 2myArray.sort(); //sorts array in ascending order 3/* Example output 4[1, 2, 4, 5, 7] 5*/ 6myArray.reverse(); //sorts array in descending order 7/* Example output 8[7, 5, 4, 2, 1] 9*/ 10

php syntax With PHP

1$myArray = array(2,5,1,4,7); 2sort($myArray); 3print_r($myArray); //sorts array in ascending order 4/* Example output 5Array 6( 7[0] => 1 8[1] => 2 9[2] => 4 10[3] => 5 11[4] => 7 12)*/ 13rsort($myArray); 14print_r($myArray); //sorts array in descending order 15/* Example output 16Array 17( 18[0] => 7 19[1] => 5 20[2] => 4 21[3] => 2 22[4] => 1 23)*/ 24$myAssosiativeArray = array("SSN-9232323" => "Ramon Cornell", "SSN-5643233" => "Steban Dido", "SSN-5554433" => "Mikelly Reik", "SSN-3423344" => "Bob Stalin"); 25asort($myAssosiativeArray); 26print_r($myAssosiativeArray); //sort associative arrays in ascending order, according to the value 27/* Example output 28Array 29( 30[SSN-3423344] => Bob Stalin 31[SSN-5554433] => Mikelly Reik 32[SSN-9232323] => Ramon Cornell 33[SSN-5643233] => Steban Dido 34)*/ 35ksort($myAssosiativeArray); 36print_r($myAssosiativeArray); //sort associative arrays in ascending order, according to the key 37/* Example output 38Array 39( 40[SSN-3423344] => Bob Stalin 41[SSN-5554433] => Mikelly Reik 42[SSN-5643233] => Steban Dido 43[SSN-9232323] => Ramon Cornell 44)*/ 45arsort($myAssosiativeArray); 46print_r($myAssosiativeArray); //sort associative arrays in descending order, according to the value 47/* Example output 48Array 49( 50[SSN-5643233] => Steban Dido 51[SSN-9232323] => Ramon Cornell 52[SSN-5554433] => Mikelly Reik 53[SSN-3423344] => Bob Stalin 54)*/ 55krsort($myAssosiativeArray); 56print_r($myAssosiativeArray); //sort associative arrays in descending order, according to the key 57/* Example output 58Array 59( 60[SSN-9232323] => Ramon Cornell 61[SSN-5643233] => Steban Dido 62[SSN-5554433] => Mikelly Reik 63[SSN-3423344] => Bob Stalin 64)*/

The Switch Statement

This is almost identical to the switch statement in JavaScript:

php syntax With JavaScript

1favcolor = "red"; 2switch (favcolor) { 3 case "red": 4 return "Your favorite color is red!"; 5 break; 6 case "blue": 7 return "Your favorite color is blue!"; 8 break; 9 case "green": 10 return "Your favorite color is green!"; 11 break; 12 default: 13 return "Your favorite color is neither red, blue, nor green!"; 14}

php syntax With PHP

1<?php 2 $favcolor = "red"; 3 switch ($favcolor) { 4 case "red": 5 echo "Your favorite color is red!"; 6 break; 7 case "blue": 8 echo "Your favorite color is blue!"; 9 break; 10 case "green": 11 echo "Your favorite color is green!"; 12 break; 13 default: 14 echo "Your favorite color is neither red, blue, nor green!"; 15} 16?>


When working with objects, we have to use the operator "->" instead of "." to access the object properties.

php syntax With JavaScript

1var auxCar = new Car(); 2console.log(auxCar.brand);

what is php tutorial With PHP

1<?php 2 $myCar = new Car(); 3 echo $car->brand; 4?>

All the other Operations

All the other operations are pretty much the same as in JavaScript. Just use the dollar $ sign at the beginning of each variable, and use the arrow to access object properties instead of the dot .