Data Types in PHP
Posted by chaabant on Dec 28, 2007
Data Types in PHP
In php there is different types of data , the most used are :
string // binary data
boolean // true or false
int // Integer
Integers are numbers; Positive and negative numbers can be expressed with it).
Boolean contain two values: false or true .
String Are text , it could be also a collection of binary data (contents of a doc file or image or even mp3 file)
Declaring Variables in PHP
To declare variables in php all what you need to do is place a $ at the beginging of a name that you will give to your variable to refer it .
ex :
1 2 3 4 5 |
$my_number = 13 ; // declaring variable called my_number $my_name = "Chaaban"; // declaring a variable called "my_name" that contain : Chaaban ; $whatever = true ; // declaring a variable called "whatever" that contain a boolean value of true . |
$my_number = 13 ; // declaring variable called my_number $my_name = "Chaaban"; // declaring a variable called "my_name" that contain : Chaaban ; $whatever = true ; // declaring a variable called "whatever" that contain a boolean value of true .
php is different than other languages , you can declare a variable that contain a number and later replace it with a string or boolean .
ex :
1 2 3 4 5 6 7 8 9 |
$x = 10 ; // random codes // ... // random codes $x = "HELLO WORLD"; echo $x ; // will display : HELLO WORLD instead of 10 |
$x = 10 ; // random codes // ... // random codes $x = "HELLO WORLD"; echo $x ; // will display : HELLO WORLD instead of 10
Invalid names when Declaring a Variable
Variables must be named using only letters (a-z, A-Z), numbers and the underscore character; Their namesmust start with either a letter or an underscore, and are one of only two identifier types in PHP that are case sensitive
1 2 3 |
$txt = ’valid’ ; // valid $_txt = ’valid’; // Valid variable name $10txt = ’invalid’; // Invalid varianle name since it starts with a number |
$txt = ’valid’ ; // valid $_txt = ’valid’; // Valid variable name $10txt = ’invalid’; // Invalid varianle name since it starts with a number
How to put comments in php
Posted by chaabant on Dec 26, 2007
In php there are different ways to put comments , comments are very important .
Importance of php Comments in programming:
- It’s a Good Practice
- It saves a lot of time , you won’t need to read your code to understand what it does
- Make your code understandable for others, so that if many programmers are working on a same project, or when your boss fires you, it would be helpful for then next programmer to understand what you have done.
Any code that took thought to write will take thought to reread after several days, months or in some cases, years.
Ok , now let’s go back to How to put comments in php …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Single line comment # Single line comment /* Multi-line Line 1 Line 2 Line 3 */ /** * API Documentation Example * * @param string $bar */ function foo($bar) { } //code |
// Single line comment
# Single line comment
/* Multi-line
Line 1
Line 2
Line 3
*/
/**
* API Documentation Example
*
* @param string $bar
*/
function foo($bar) { } //code
Hello world in php
Posted by chaabant on Dec 26, 2007
Those are some ways to make a Hello world in php
Hello world in php using Variables
Hello world in php using Functions
1 2 3 4 5 6 7 8 9 10 |
<? function hello () { echo " Hello World !! "; } hello(); ?> |
<?
function hello () {
echo " Hello World !! ";
}
hello();
?>