PHP fundamentals – introduction, php instructions and variables

Hey guys, going further with our tutorial today. We are going to talk a bit about php instructions and variables and how they help us get things done. If you ended up here for the first time, be sure to check out our simple PHP introduction.

PHP fundamentals – Introduction

Normally, any computer can process data much better than us and for doing that, any program out there requires some fundamental elements like: a way to specify the location of the data we want to process, a way to specify type of the data we want to process (number, string, etc) and a way to make operations with those data.

This 3 necessities are usually specified in PHP using the following notations:

  • variables – data names that help the programmer to specify or assign a thing (number, string, etc) to a specific name (or label) that he wants to put in his program
  • data types – variables usually can have multiple data types. You can use numbers, true or false values, chars, etc.
  • operators – syntax elements which applies an operation to one or multiple variables producing a result (lets say you have a variable $a = 1, and a second variable $b = 2 … $c = $a + $b ($c will equal 3 because is the sum between 1 and 2)

PHP fundamentals – PHP instructions

A PHP program is usually build from a series of instructions. There can be 2 types of instructions:

  • simple instructions – usually instructions that can be written on a simple line. Instructions always end up with an ; after
  • advanced instructions or blocks of instructions – a series of instructions that usually start with { and finish with } (check examples below)

Simple instruction

<?php 
echo "simple instructions "; 
//notice the space, if we echo 2 strings without using 
a <br />, they will be on the same line 
echo "we want to learn";
// output of this is "simple instructions we want to learn"
?>

Advanced instructon

<?php
if($first == 4) {
    $first = 10;
    echo "$first was reseted to 10";
}
?>

In this, we check if the “$first” variables is equal to 4 and if it is, we changed it to 10 and print “$first was reseted to 10″. Pretty simple right? DON’T FORGET: You can always comment a piece of code using “//” or comment blocks of code using “/* */” (see bellow).

<?php
// commented piece of code
/* commented blocks
of
code */
?>

PHP fundamentals – More on Variables

Lets elaborate a little bit. Variables are memory zones that stock data that the programmers refer to when using a name (label). When the program parses the code, the variables are stocked in memory, in a different place every time.

Variable names

A variable name always starts with the dollar sign ($ – like jQuery), followed by a set of numbers, chars or underscore (_). Its mandatory that the first character after the $ sign to be underscore or a letter.

<?php
$var = 1;
$secondVar = 2;
$_thirdVar = 4;
?>

For good practice, its common to use camel case notation like $variableName. Some invalid variables name can be found bellow.

<?php
$6 = 1;
$variable-name = 1244;
?>

You can’t refer to a variable if it was not added before the execution or dealing with it. Meaning, if we add a variable $a = 4, and we “echo $b”, we will get an error because $b was not defined yet. So you first must declare all your variables and afterwards work with them. You can easy delete a variable using “unset”.

<?php
$me = 12;
unset($me);
echo $me; // $me is not defined. 
?>

Variable types

As we said before, there are a couple of data types that variables can take. We can use data number variables that can be “int” (only full numbers) or “float” (numbers with decimals). We can also use true or false (usually called boolean data types) or we can use character strings (called string).

The data type can change along the way when executing a program, transforming from data number variable to string and so on. To get the data of a variable you can always use “gettype” (see bellow).

<?php
$variable = 12;
$secondVariable = "string me now";

echo gettype($variable); //will output integer
echo gettype($secondVariable); //will output string
?>

//also, if you define a variable and give it 
the "true" property, it will return 1 (example)
$a = true;
echo $a //returns 1

You can output a variable in different ways and we already seen that echo does the job perfectly. You can also use “print” or “print_r” but the second one is manly used for output of arrays. There is also “var_dump” for array but I haven’t seen it used to many times. Lets see some examples.

<?php
$first = 1; $second = 3;
echo ($first); // output is 1
print ($second); // output is 3
echo $first, $second; // output is 1,3
print $first, $second; // error because it does not accept multiple arguments
echo (print($first)); // first the print is executed and afterwards the result from the print
print(echo($first)); // error because echo is not a function and does not return anything
?>

The “print_r” returns or prints the value of the expression and does not print the type of data of the expression. Its mainly used for printing out array tables. Check example bellow.

<?php
$arrayMe = array(1,3,5);
echo $arrayMe; // Array
print_r($arrayMe); 
// Array
//   (
//     [0] => 1
//     [1] => 3
//     [2] => 5
//   )
?>

Keep in mind that arrays always start with 0 and not 1, so if you want to get the first value of an array use $arrayMe[0].

The “var_dump” bit does almost the same as “print_r” but instead it points out also the data type of the value. Like, looking at the example above we should have something like “[0] => int(1)” and so on.

Besides variables that we define, there are also variables that are predefined. They usually contain information regarding data about the client/user that accessed the webpage but also important information regarding the server and connexion. This kind of variables are usually called “superglobals” and the full list can be found here. Some examples bellow.

<?php
$_SERVER // infos related to server, HTTP headers, etc
$_GET // infos about some bits of data from the URL...
// Example: We have http://www.example.com/index.php?me=1&you=2
// The $_GET gets the ?me=1&you=2 and you can access them in your program
$_POST // info related to the variables passed to the PHP program trough HTTP POST command
$_SESSION // info related to user session.
?>

Related Posts

PHP fundamentals – PHP constants, PHP data types

What is PHP? The Beginnings.

Cool forum software written in PHP and MySQL

Visualize time series easily with Cubism.js

Leave a Reply