Learn PHP from Scratch Day 3
This tutorial is actual start of the PHP code, in last lecture we wrote simple PHP Hello World program , in this tutorial we start to learn PHP Data Types, Variables, Constant &Operators. As like any other programming language PHP also have variable, constants & operators. Let’s dive in to deep of those concepts in consideration of PHP.
PHP Data Types
Data types are types of variables , i.e. what type of data variable can save or assigned. Some times we need to save name of user or any other text , then we need different data type, where as to save date, age , file etc. each time we need to have different data types. Here are some data types below;
- Alphanumeric characters are classified as strings
- Whole numbers are classified as integers
- Numbers with decimal points are classified as floating points.
- True or false values are classified as Boolean.
PHP is a loosely typed language; what does mean by loosely typed language? it means PHP does not have explicit defined data types. PHP determines the data types by analyzing the attributes of data supplied. You don’t need to define data type of variable while defining it. Just like other some programming language needs to define variable type with variable like its string, its integer etc. That’s not case with PHP. We will see more about data types supported by PHP , first we discuss about PHP variables..
A variable is a name given to a memory location that stores data at runtime.
The scope of a variable determines its visibility.
A Php global variable is accessible to all the scripts in an application.
A local variable is only accessible to the script that it was defined in.
Think of a variable as a glass containing water. You can add water into the glass, drink all of it, refill it again etc.
The same applies for variables. Variables are used to store data and provide stored data when needed. Just like in other programming languages, PHP supports variables too. Let’s now look at the rules followed when creating variables in PHP.
- All variable names must start with the dollar sign e.g. $My_var
- Variable names are case sensitive in PHP; this means $a is different from $A
- All PHP variables names must start with a letter follow other characters e.g. $a1.While $1a is not a legal variable name.
- Variable names in PHP must not contain any spaces, “$user name” is not a legal variable name. You can instead use an underscore in place of the space. Also can use minus sign to generate variable names instead of space. E.g. $last-name, $last_name are legal variable names.
Let’s now look at how PHP determines the data type depending on the attributes of the supplied data.
<?php $k = 10; echo $k; ?>