PHP: The lamborghini Language
In the Beginning
In the 1990's, when the internet was becoming a common occurrence, the most common way to design a website was to create a static website. This involved creating websites in HTML. The websites had one drawback, they could not be used to perform some features that are being performed by today's back-end technologies. To be able to combat this, some of the earliest technologies used the Common Gateway Interface(CGI). CGI was a protocol that allow external applications to interact with the server. Due to their complexity, they were later preceded by their implementations in programming languages like Perl, ColdFusion and Active Server Pages just to mention a few.
The birth of the "Lambo" Language
Just like every other technology out there, the Benevolence Dictator for Life for PHP is Rasmus Lerdorf.
He created the language to replace the CGI scripts that he would write in C (source was Gemini). The language was a huge hit among web developer helping to create some of the largest websites and close 60% of all the content in the web. Some of the websites include Facebook, Wikipedia, WordPress, Slack etc.
PHP has undergone several iterations to the latest version 8. In the years it has gone through some changes to become the language that is today.
My introduction to the "Lambo" language
I began my coding journey in C. For my earliest languages, the main thing that was important was the compiler. This will compile the code for me to the desired output which will then be run by the compiler. It was not until later that I learnt how to run my compiled program outside the IDE. The first interpreted language that I learnt was python. This I at least had a clear vision of how it was interpreted.
I did some PHP briefly before and I found it a hard language to work with as I could not get how to run my code. I did not understand how to put the PHP tags in a PHP file as they all looked like HTML. Sometime later I was introduced to the Giraffe channel in YouTube which gave me the courage to go ahead. Below is the first video I watched
Like most things in programming, I was a victim of the propaganda in the internet. A place where everybody tried to justify every language as either good or bad. One of the reasons why I had not tried the language before was because, I had heard from various videos and blogs how PHP was a dying language and it had a horrible syntax.
"Show me a language that is perfect and I will show you a language that does not have any users", Bjarne Stroustrup.
Honestly speaking coming from a different language like python, C++, C and Java, PHP is kind of weird. Some of its weirdness comes with the way that a variable name is presented i.e. .
$hello = "hello";
This weirdness I think is associated with the fact according to my opinion it may have been built from Perl as Perl has the exact same syntax. Other than that, PHP is just like any other language.
Few basics of the language.
- conditions
<?php
// Basic if statement
$age = 18;
if ($age >= 18) {
echo "You are an adult.\n";
}
// if...else statement
$age = 15;
if ($age >= 18) {
echo "You are an adult.\n";
} else {
echo "You are a minor.\n";
}
// if...elseif...else statement
$number = 5;
if ($number > 10) {
echo "Number is greater than 10.\n";
} elseif ($number > 5) {
echo "Number is greater than 5.\n";
} else {
echo "Number is less than or equal to 5.\n";
}
// switch statement
$day = "Monday";
switch ($day) {
case "Monday":
echo "Today is Monday.\n";
break;
case "Tuesday":
echo "Today is Tuesday.\n";
break;
default:
echo "Today is some other day.\n";
}
// Ternary operator
$isRaining = true;
$message = $isRaining ? "It's raining." : "It's not raining.";
echo $message . "\n";
<?php
// Create an array of fruits
$fruits = ["apple", "banana", "orange", "grape", "kiwi"];
// Count the number of fruits
$fruitCount = count($fruits);
echo "Number of fruits: " . $fruitCount . "\n";
// Add a new fruit to the end
array_push($fruits, "mango");
echo "Fruits after adding mango: " . implode(", ", $fruits) . "\n";
// Remove the last fruit
array_pop($fruits);
echo "Fruits after removing the last fruit: " . implode(", ", $fruits) . "\n";
// Remove the first fruit
array_shift($fruits);
echo "Fruits after removing the first fruit: " . implode(", ", $fruits) . "\n";
// Add fruits to the beginning
array_unshift($fruits, "pineapple", "strawberry");
echo "Fruits after adding pineapple and strawberry: " . implode(", ", $fruits) . "\n";
// Sort the fruits alphabetically
sort($fruits);
echo "Sorted fruits: " . implode(", ", $fruits) . "\n";
// Reverse sort the fruits
rsort($fruits);
echo "Reverse sorted fruits: " . implode(", ", $fruits) . "\n";
// Create another array of vegetables
$vegetables = ["carrot", "broccoli", "potato"];
// Merge the two arrays
$combined = array_merge($fruits, $vegetables);
echo "Combined array: " . implode(", ", $combined) . "\n";
<?php
// While loop
$i = 0;
while ($i < 5) {
echo $i . "\n";
$i++;
}
// Do-while loop
$i = 0;
do {
echo $i . "\n";
$i++;
} while ($i < 5);
// For loop
for ($i = 0; $i < 5; $i++) {
echo $i . "\n";
}
// Foreach loop (for arrays)
$fruits = ["apple", "banana", "orange"];
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
<?php
function greet($name) {
echo "Hello, " . $name . "!\n";
}
greet("Alice");
greet("Bob");
// Classes
class Person {
public $name;
public $age;
public function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
public function introduce() {
echo "Hello, my name is $this->name and I
Finishing
While PHP may have looked liked a falling language, it is making a comeback through Laravel, Symphony and Sinatra, just a few frameworks. PHP 8.x.x is also forms the basis of WordPress. It may be unwise to dismiss learning or working on it as it seems an old language with a bad PR.


Comments
Post a Comment