Hi! This is the second post of my PHP series. In here I’ll talk about the basics of PHP, comparing it with Java and JavaScript (mainly, since these are the languages we learn and use in SAIT).

In case you missed last blog post, I recommend reading it before starting this.

Getting ready

Since PHP is used on Web applications, you first need to get a web server (local or not) so you can test.

When using a local web server on your machine or on a Raspberry Pi (tutorial here), you just need to install PHP by downloading and installing the files from php.net. Here is a direct link to their download page. It works with Apache, IIS, etc… in OSX, Windows, Linux, etc…

When using a paid web server, you have to check with them if they have PHP installed. But considering that PHP is light and free, as you saw on last post, there is a high probability it will be installed by default. This blog is hosted on Blue Host, which does. There is also the fact that WordPress, the main blogging interface nowadays, uses PHP.

Ok, now that you have your server set, what else do you need? Nothing. You are ready to go!

Getting started

What about writing “This was made in PHP” and printing it on the page? To do that, create a text file, save it with a .php extension and type.

PHP is inserted basically the same way as we insert JavaScript into a HTML file, with a opening and a closing tags, but in this case, they are “<?php” and “?>” respectively.

<!DOCTYPE html>
<html>
<body>

<?php
echo "This was made in PHP";
?>

</body>
</html>

The result is:

This was made in PHP

As you can see here.

If you inspect this element (right click -> Inspect or Ctrl+Shift+I on Chrome, right click -> Inspect Element (Q) on Firefox, or right click -> Inspect Element on Internet Explorer), you will see a plain HTML page.

Both “echo” and “print” are similar to the Java “System.out.println”, and can be used to send html lines of codes. You can even use HTML tags on the echo command.

<?php
echo "<strong>This was made in PHP</strong>";
?>

The result is:

This was made in PHP

As you can see here.

Comments

Well… I’ll not spend much time in here, because it’s exactly like Java and JavaScript, where a double dash means commented line and dash-star opens a comment block, that star-dash closes.

// I'm a single line comment
/*
I am
a multi-line
comment
block
*/

Variables

PHP is a weak-type language, much like JavaScript. To start a variable, simply use the dollar sign.

<?php
$grade = "5/7";
$def = "perfect score";
echo "PHP is a $grade! A " . $def . "!";
?>

The result is:

PHP is a 5/7! A perfect score!

As you can see here.

We can use the variables directly inside the strings, as you can see on the “echo” part, or concatenating using a single point. It is also possible to use the variables in calculations.

<?php
$num1 = 10;
$num2 = 2;
echo "The result of 10 * 2 is " . $num1 * $num2;
?>

The result is:

The result of 10 * 2 is 20

As you can see here.

Functions

We can create functions on PHP, thus making the variables inside and outside of these functions local scoped ones.

Functions, just like in JavaScript, are similar to Java’s methods, but again weakly used. To create one, just type “function”, an alias, the parameters and curly braces to set the begin and the end of the function.

It is possible to, inside a function, access a variable from outside using the keyword “global” when declaring it, or by using the array $GLOBAL. This is part of a set of superglobals, and more information about all of them can be found here.

In this next example, I’m using both ways to deal with local and global variables, printing them all on the screen:

<?php
$num1 = 10; //outside variable
$num2 = 2; //outside variable
$num3 = 5; //outside variable

function myFunc() { //create a function
 global $num1; //global outside/inside variable
 $num2 = 4; //inside only variable
 $num3 = 7; //inside only variable
 echo "<p>The first number inside the function is the global num1: $num1</p>";
 echo "<p>The second number inside the function is the global num2: " . $GLOBALS['num2'] . "</p>";
 echo "<p>The third number inside the function is a local num3: $num3</p>";
 
// Changing the variables
 $num1 = $num1 * 100; //changes the global outside/inside variable
 $GLOBALS['num2'] = $GLOBALS['num2'] * 100; //changes the outside variable
 $num3 = $num3 * 100; //changes the inside only variable
}

myFunc(); //call the function
echo "<br><br>";
echo "<p>The first number outside the function is the global num1: $num1</p>";
echo "<p>The second number outside the function is the local num2: $num2</p>";
echo "<p>The third number outside the function is a local num3: $num3</p>";

?>

The result is:

The first number inside the function is the global num1: 10

The second number inside the function is the global num2: 2

The third number inside the function is a local num3: 7



The first number outside the function is the global num1: 1000

The second number outside the function is the local num2: 200

The third number outside the function is a local num3: 5

As you can see here.

A variable can only be accessed as global if it was declared outside a function. Trying to declare a new variable inside a function as global will result into an error. Trying to call a variable that was declared inside a function from outside using the $GLOBALS[], will return an empty value.

PHP can handle the following data types:

  • String
  • Integer
  • Float/Double
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

In Java, we are used to strong -typing our variables, always telling the computer the type of the variable. Since PHP is not like that, if you want to make sure what type the variable was created, you can use the function “var_dump($var);”

<?php
$var = 3.1415;
var_dump($var);
?>

The result is:

float(3.1415)

As you can see here.

There are a lot of other functions available on php API, but I’ll talk about them in the next post.

Well… that’s it for today’s post. I’ll soon talk about the Array, If/Else/Elseif, Switch and Loops.

Thank you very much and I hope to see you soon.

Thiago

 

References: w3schools