Basic Data Types

Numbers (integers and real)

<?php
    $a = 1234;
    $b = 0777;
    $c = 0xff;
    $d = 1.25;
    $e = 10e5;
    echo "$a $b $c $d $e<br />\n";
?>

Strings

<?php
    $a = 'can'; $b = 'forms';
    $str1 = 'Let\'s use literals.'; // Single-quoted
    $str2 = "\nEmbeded vars {$a} come in many $b.\n";   // Double-quoted
    $str3 = <<<HEREDOC
we even support Perl like syntax.
HEREDOC;

    echo $str1.nl2br($str2).nl2br($str3);
?>

String QnA

  1. If you are using single quote in your string like above for $str1 , than you need to escape your single quote in your sentence with "\" trailing slash, so that php is not confused where your string ends. If you don't escape the single quote from your sentence , than php would assume, your string has finished on second single quote found anywhere in your sentence and rest of them will be out of single quote, which will throw an error.
  2. So, is applied with Double Quote too, if you have double quote in your stirng and you are using double quote like in above for $str2 ,and your sentence has double quote, than you need to escape dopuble quote from your sentence too using "\" .
  3. Important note: Any Variable passed inside single quote like 'Hello $a, How are you ?' are not parsed, i.e, it will print $a only in output.
  4. But any Variable passed inside double quote like "Hello $a, How are you " are parsed, and it will print value of $a variable as specified. But if variable has not been created earlier, than PHP will throw an error. Variable not defined.
  5. It is highly recommended to use curly braces with variable name , so that it is convenient for other fellow developer like "Hello {$a} How are y ou ?" with curly braces arround $a , you differentiate, it is variable, that ends after "a".
  6. Important Note : If you are using "<<<" syntax for your string, than , make sure, where ver your  "HEREDOC" ends, it does not have any SPACE before or after. It should be in single line and there should be no other letter except ";" (semi-colon).
  7. nl2br is function in php, which converts "\n" to HTML "<br>" format

Output of Above "echo".

Let's use literals.
Embeded vars can come in many forms.
we even support Perl like syntax.

Booleans

<?php
    $greeting = True;
    if ($greeting) {
        echo "Hi There!";
        $greeting = FALSE;
    }

// Booleans are 0 , 1 or true , false

    $greeting = True;
    while ($greeting) {
        echo "Hi There!";
        $greeting = FALSE;
    }


?>

PHP is Dynamic Typing

 You don't have to declare variable types i.e, "int, char" etc., it is done automatic for you.

<?php
    echo 5 + "1.5" + "10e2";
?>

 


Loading ...

About the Author