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. $my_number = 13 ; // declaring variable called my_number
  2.  
  3. $my_name = "Chaaban"; // declaring a variable called "my_name" that contain  : Chaaban ;
  4.  
  5. $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. $x = 10 ;
  2.  
  3. // random codes
  4. // …
  5. // random codes
  6.  
  7. $x = "HELLO WORLD";
  8.  
  9. 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. $txt   = ’valid’ ;  // valid
  2. $_txt  = ’valid’;   // Valid variable name
  3. $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:

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. // Single line comment
  2.  
  3.  
  4. # Single line comment
  5.  
  6.  
  7.  
  8. /* Multi-line
  9. Line 1
  10. Line 2
  11. Line 3
  12. */
  13.  
  14.  
  15.  
  16. /**
  17. * API Documentation Example
  18. *
  19. * @param string $bar
  20. */
  21.  
  22. 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

  1. <?
  2. echo " Hello world !" ;
  3. ?>

Hello world in php using Variables

  1. <?
  2. $txt = "Hello World !" ;
  3.  
  4. echo $txt ;
  5. ?>

Hello world in php using Functions

  1. <?
  2.  
  3. function hello () {
  4.  
  5. echo " Hello World !! ";
  6.  
  7. }
  8.  
  9. hello();
  10. ?>