Variablees, constants and data types in php5
lang_of_article_differ
want_proper_trans
Introduction
This course requires from you basic programing knowledges such as variables and control-flow structures but, if you don't know what this words means - its not a problem. To learn programming is easier than learn to drive a car. As for me - i've have never driven a car.
variables in programming languages like pages in your notebook: every page have their own type of writings you made and size: you can't write more text on a page than the size of this page. Also, every page has its unique number and sometimes you corefer to the another page number in your notes.
The same situation in programming: every variable (blank page) have unique name (page number), type (drawings, text, calculations, and so on) and size (physical page dimensions). When you consider some variable to hold your data (you decided to write university timetable to page 12) you know what type of note it would be : list of days with time, room number and subject name: same situation in programming.
Php is a weak typyzed programing language. This means you can ommit variable's type or change it on the fly: at first in your variable was a number and then you write down the sentence to it - that's not a problem.
Variables in php can be declared in any part of a program. Moreover its not obligated to declare variable before usage.
Recommendations
Before writing any line of code when you test some code, please write down next two lines of code at the beginning of the php file as
<?php
ini_set('display_errors','On');
error_reporting(E_ALL);
// your code goes here
Its options which will say to php interpeter to output any errors and notices to the screen. Its very usefull because sometimes there are situations when nothing appears on screen but some errors happen and you don’t know what it is.
Comments
In php code there can be a user comments which not took into consideration when going to execute a program: There are three types of comments: single line comments starting with // or # sign:
<?php
// this is a single line comment
# this is a single line comment too
/* this is very long
multiline comment */
Multi line comment start with /*
and ends with */
Variables
Variables declared by the dollar sign at the beginning of the name and not empty variable name which follows dollar sign:
$variable;
$x = 10;
$oneMore = 'variable';
Name of a variable shoud follow right after dollar sign:
$ y = 5; // syntax error
Variable declarations can be separated by comma:
$variable, $another_variable;
ts allowed to use almost any utf-8 character in variable name except php’s special characters: , but sincerely asked to use English alphabet and some special characters like underscore:
$переменная = 5; // bad, but working
$variable = 5; // good
$_variable = 5; //good
Variables free to alter at any moment of a program:
$x = 5;
$y = 0;
// some code goes here
$x = $x+5;
$y = $x;
Constants
If you want to get non modificating variable you should declare a constant. Its commonly accepted to use only uppercase letters in constant naming. Take a look that defining a variables in php completely differ from variables:
define('SOME_CONSTANT','some value');
To access a constant you shouldn’t use a dollar sign too:
echo SOME_CONSTANT;
echo $SOME_CONSTANT; // this variable has not a constant value if its exists
Also, you can’t change the value of a constant at program after you had declared it:
SOME_CONSTAT = 5; // will result in syntax error
Data types
Php has 7 data types:
4 scalar types including integer
, double
, string
and boolean
.
3 complex data types including array
, object
and resource
.
Integer
Integer is a type of a integer numbers like -15, 1,2, 545, and other. Maximum values is a 32 or 64 bit value related to which version of php you are using.
$x = 1; // the variable of an integer type
Dobule
Double type is a type of real numbers. Its commonly the same as integer.
Operations supported by the integer and double variables are the follows:
$x = 5;
$y = 10;
$summ = $x + $y; // summ operation
$summ = $x - $y;
$mult = $x*$y; // multiply operation
$div = $y/$x; // division operation
there are very usefull operator add and assign, multiply and assing and others which can substitute next opeartions:
$x = 5;
$x = $x+5; // adding a 5 to a previous $x value
echo $x; // 10
We can rewrite operation $x = $x + 5; by $x += 5; there must not be a space between + and =
$x += 5;
echo $x; // 15
Another similar operator are *= (multiply and assign), /= (divide and assign), -= (add negative and assign). Also, there are many mathematical functions included in php like pow – power, sin, cos, sqrt and other but its not in the scope of this particular topic.
Strings
String type used to hold strings and characters:
$name = 'John';
You can concatenate (glue) strings with dot operator (.):
$fullName = $name.' Surname'; // concatenation of a string and variable
Lets work with some most usefull string funcions. They are strlen for getting the length of a string and substr for getting a substring of a string.
$label = 'Hello World';
echo strlen($label); // 11
echo substr($label,0,5); // Hello
Substr used to get the substring of a string from $startOffset to $endOffset. substr(string $s, int $startOffset, int $endOffset = null). You can ommit the $endOffset param and get the substring from startOffet up to end of a string:
echo substr($label,6); // World
Also you can use negative offsets:
echo substr($label,0,-3); // Hello Wo
The most usefull function to work with strings is explode, but it working with arrays, so we would take a look at furhter.
Boolean
Bollean datatype consists of two values true and false and used most commonly in the control flow structures. Also boolean data-type can be used as a flag:
$hasProperty = true;
Null type.
Null type used for only one value of null. Null means nothing. If you’re common with another languages like c++ or java you already know what it is.
Array
Please read about arrays in article related to arrays in this course
Objects
Object is quite the same as array but completely another :) Objects are the instances of some class, in other words they are the variables of some complex data type which is created by the user. We will look at this topic deeply in the next lessons.
Resource
Resource data type is a little bit outdated now and used only in some cases when you want to access some special features of an operational systems or databases. As in case of objects, lets come back to this topic later.
Getting the type of variable
Sometimes you might want to get the type of some variable. You can get the type of a variable by calling function gettype with an argument – variable of which you want to get a type:
$x = 5.05;
echo gettype($x); // double
$z = true;
echo gettype($z); // boolean
$n = []; // or, with older version of php: $n = Array();
echo gettype($n); // array
Checking if some variable exists
You can check whether some value exists or not by isset function. Addinationally, you can check is it empty (its value equal to “”,0,[],or null) or not with empty function:
if (isset($a)) {
echo “a variable exists”;
if (!empty($a)) {
echo ' and its not empty';
}
}
Removing variables
You can remove or, better say unset, variables with unset function:
$x = 5;
unset($x);
echo $x; // undefined variable
Conclusion
For the first time its more than needed – to know about scalar types and arrays. On the way we will get acquainted with more and more complex and interesting things.