ckportfolio.com - Element of Programming with PHP

Element of Programming with PHP

Comparing PHP to JavaScript

Upon experimenting with various programming languages, one comes to a realization that they actually have a lot in common: they all seem to feature variables and functions to store and process information, and their syntaxes, although not identical, are quite similar. Just like how we feel about different spoken languages around the world, it is not rare to find programming languages that bear striking resemblance to one another.

JavaScript and PHP are inherently designed for two distinct purposes, but both feature all attributes common in today's programming languages. This article showcases how PHP (slightly) differs from JavaScript when handling various elements of programming.

Variables

Each variable is declared with a $ sign when using PHP. While JavaScript uses the + sign for both addition and concatenation, PHP uses a period (.) to combine strings together.

$x = 2;
$y = 5;
$z = x + y;
// $z = 7;

$a = "Hello";
$b = "World"
$c = $a . " " . $b;
// $c = "Hello world";

On PHP, arrays are created by calling a basic function called array() with one or more parameters that would serve as initial array entries.

$x = array('BMW', 'Volvo', 'Hyundai');

While PHP did ultimately receive the object data type in later versions, the equivalent could be created using "associatve arrays" where one creates an array with custom, non-sequential "keys" to corresponding values.

$person = array("name" => 'John Doe', "email" => 'john@doe.com', "phone" => '6471234567');

In comparison, the three names in the $x array have sequential keys 0, 1, and 2 respectively.

Function

One can also declare functions in PHP to reduce redundancies and combine frequently used lines of code into a single entity. PHP offers a very similar mechanism as JavaScript:

function sum($a, $b) {
    $answer = $a + $b;
    return $sum;
}

$answer = sum(5,7);
echo $answer;

Aside from syntax and lack of alert() function (as PHP does not have control over browser functionality), the above function is almost identical to its JavaScript counterpart. Instead of alert(), the sum function returns a value, which is then printed using echo or a pair of shorthand tags (<?= and ?>).

Operators and Conditional Statements

These elements are also very much similar to JavaScript. prompt() also is a JavaScript-exclusive function, and can be replaced with a reference to submitted form data:

var response = $_POST["answer"];
// processing submitted form data

if ($response == 14) { // if the response equals to 14
    echo "You are correct!";
} else if ($response == 5) { // if the response equals to 5
    echo "Wrong. Perhaps you were trying to subtract 2 from 7?";
} else { // otherwise...
    echo "Wrong!";
}

Loops

PHP also supports a standard for loop to run one or more tasks for a finite number of times. Alternatively, we can use a foreach that simply takes an array, automatically calculates the finite number, provides each array entry throughout the looping process.

$cars = ["BMW", "Volvo", "Saab"];
$text = "";

for ($i = 0; $i < count($cars); $i++) {
    $text .= $cars[$i];
}

// OR ...
$text = "";

foreach ($cars as $i => $name) {
    $text .= $cars[$i];
}

Try and Catch

Unlike JS, try and catch on PHP is not designed to catch all errors in the application, but to design functions to react to unexpected situations. Functions can "throw" an error when a certain condition is or is not met, and the try-catch mechanism successfully "catches" the error and respond accordingly.

function checkScore($number) {
    if($number > 3) {
        throw new Exception("Function cannot accommodate numbers beyond 3. Function received ".$number);
    }
    return true;
}

$answer = 5;

try {
    checkScore($answer);
} catch (Exception $ex) {
    echo $ex;
}
Fin