Archive | php-lesson1(variables)

Echo and print are used to output in PHP

these two functions are used to output the result of the statement you defined to them to the browser.Echo and print functions unline other function structure can be followed even with or without the paranteses.

Example of using Echo:

echo "this is without parenteses";

or with the parenteses thi the same result;

echo ("this is with parenteses");

if you wanted to output multiply parameter using echo it also can be done:

echo "My", "Name";

but the function echo will produce an error when you try to use multiply parameter in using it in parenteses version
so the following will produce error:

echo("My", "Name");

Print:
this function unlike echo accepts only one parameter
Example:

print("My Name");

or

print "My Name";

Posted in php-lesson1(variables)0 Comments

String Type in PHP

String Type
a string is simply a sequence of characters treated as a contiguous group.Such groups are
typically delimited by single or double quotes,
String Examples:

$str_sq = 'single quat string';
$str_dq = "double quat string";
$str_zc = "";//string with zero characters;

as I mentioned a string can be enclosed both in single and double quotation marks,that each will behave diffrently.singly quoted strings are treated literally meaning that if there were any variables inside it ,the name of the variable will be printed instead of the value of it, but double quoted strings will reaturn the value of the variable with printing of sequence of characters near it.

single quoted Strings
Escept some characters, singly quoted strings will read and stroe their characters literary, meaning that if there is a varaible inside the string value, it will print the variable name with dollar sign itself rather than returning its value.
Example

$Literally = 'This $variable wont have its value returned';
print($Literally);

Outputs:

This $variable wont have its value returned

nesting quotations in string
There are Times where you want to have one wod to be emphasized or to isolate a word from the body of your written sentens that will be accomplsihed so by using quotes by as the quotes are in the PHP language structure, How to nest it?
if you want to do so first you must see if you wat the string to be read litterally
and if you want a word inside your sentence to be quoted, the quotation must be vary from the quotation used for the uper leve
forexamlpe if you are using the signle quotation then you should use the double inside for litteral strings
if you are tring to use double qoutation for isolate or emphasise a word then you must use the single quotaton on the uper level
Example:

$single_quote = 'This contains "double-quotes" in this string but single qoute for the variable';
$double_quote = "This contains 'single-quotes' in this string but double qoutes for the variable";

there is also another way to have both quotes for the variable string and inside of the variabel that could be accomplishde by using the \’ and if you are trying to use double quotes you should use \” inside the string
Example:

$single_quote = 'This contains \'single-quotes\' both for variable and insdoe of the string';
$double_quote = "This contains \"double-quotes\" both for variable and insdoe of the string";

Provides:

This contains 'single-quotes' both for variable and insdoe of the string
This contains "double-quotes" both for variable and insdoe of the string

Posted in php-lesson1(variables)0 Comments

NULL Type in PHP

The Type NULL has only one possible value which is the value NULL.To give a variable the NULL value you should simply assign it such as follows:

$MyVariable = NULL;

the NUL type is capitalized by convention, but in fact it is incasesensitive. you can give the NULL type in lowercase letter too
such as follows:

$MyVariable = null;

so what doess NULL really mean?
actually the term “NULL” is nothing.NULL does not mean zero or blank space.It means no value or nothing.
ther is a diffrence between a variable that has not yet been assigned and a variable that has a null value

The ways that a NULL value would be created

  • It has been specifically assigned the value Null.
  • It has been erased using the function unset().
  • It has not been set to any predefined value.

Posted in php-lesson1(variables)0 Comments

Boolean Types in PHP

Boolean contains TRUE or FALSE value, which is specificly used in if statements that is one of the baces in control structure.
Boolean provides constants that are TRUE and FALSE

Interpreting other Types as Booleans

there are also some cases that are interpreted as TRUE or False values when they act like a Boolean type such as using them in if statements.

  • if the value is a number it is true if it is a number exept zero
  • if the value is a string it is true exept if it has zero of charachters or it is a charachter containing just this”0″
  • values of type NULL are always false
  • if the value is compund type such as array or an object of a class it is TRUE if it has a value and it is false if it has no other values .for an object of a class containing a value means having a member variable that has been assigned a value

Examples
True

$TrueNumber = 5 + 0.345;
$TrueString = "this contains strings of charachters";
$TrueArray[5] = 77;

False

$FalseNumber = 0;
$FalseString = "";
$FalseArray = array()
$FalseNull = NULL

Posted in php-lesson1(variables)0 Comments

Double Type in PHP

doubles are floating point-numbers
I wil exmaple a double
Example:

$My_double = 385.874;
$My_double2 = 1.5;
$My_double3 = 0.556;
$My_double4 = 9.0;
$My_double5 = 0.00003;

when doubles are having a rounded number it will not save it as an integer, and will keep its type of format as a double

$another_double = $My_double4 + 10;

that will produce a doule number that is 19.0 while the 19.0 and 19 hasthe same meaning but the type of format where it has stored in the meemory is diffrent from each other

another point to tell is that the doubles store the minimal decimal value meaning that
19.00 wont be stored and the minimum is 19.0
Exmaple:
[cc lang="php"]
$My_d1 = 1.999;
$My_d2 = 0.111;
$The_Sum = $My_d1 + $My_d2;
print(“$The_Sum”)
the result is:2.0 instead of 2.000

voshka

Posted in php-lesson1(variables)0 Comments

Integer Type in PHP

Integer types are simple, the contain the simple positive and negetive numbers and they can both assigned directly or bypassign an expression to a variable
Example:

$Myint= 684
$sample number = 8496-48683

yo can also assigne other integer bases instead of base 10 to a variable
for assigning base 8 just ad miness(-)behind the number base 8 or for base 16(hexadecimal) add “0X”followed by the number such the following

$Myintb10 = 5864
$Myintb8 = -00100
$Myintb16 = 0X1000

PHP supports the integer numbers in the range between 2^31-1 and -(2^31-1)

Posted in php-lesson1(variables)0 Comments

Variable Types in PHP

There are totally eight type is PHP;integeres, double, Booleans, strings, arrays, object, NULL and resources
here is to have a summary of all of these variables that PHP contains

  • Integers are the whole numbers without a decimal point like 34956
  • Doubles are floating-point-point numbers like 3.14159 or 56.0
  • Boolean has only two values
  • NULL is a special type that has only one value: NULL
  • Strings contains characters
  • Arrays are the name and tndexed collections of other values
  • objects are instances of programmer-defined classes that contains a package of a combination of other functions and variables that has their own values defined
  • Resources are special variables that hold refrence to resources external to php)such as database connections)
  • it might have looked like so intresting that PHP has just these 8 typpes while other programming languages such as AS and C has a numbers of Types that is so hard to remmber

    Posted in php-lesson1(variables)2 Comments

    Constants in PHP

    PHP offers constants which their value will not be reassigned regardless of ehat variables do so their value will be consistent.
    Constants definitions: Constants do not have $ before their name and by convention they will be named with upercase letters.Constants have global scope which can be accessible within the function or even any where in your scripts after that they defined.
    Example:

    define(MYNAME, "Jhon");

    Posted in php-lesson1(variables)0 Comments

    Variable Scope In PHP

    Scope is a technical term which exist in every programming language, that provide a rule for name of the variables has the same meaning in two difrent places

    any php variable not inside a function has global scope,meaning that if you assign avariable inside of a PHP file it will have the same meaning for the rest of the file, but if you assignethe variable inside the function it will loose its value when it go out of the function execution.

    when you assign a variable inside a PHP file it will not affect other variables with the same name in other PHP files.Forexample lets say you have a file next_page.php and the toher footer.php and we have a variable $username that is in one of the lines in footer.php in the body of the file so it has the global scope for that file
    $username = “mike”
    so you my think after visiting that footer.php page and then going to the nex_page.php, the value of the variable will still existed.but you are wrong, because each time a PHP page executes and assigns and reassigns variabels, those variables disappear at the end of the page.

    so assigning variables in one page will not affect the variables with the same name in other pages or even for other reuests for the same file

    there are some situations you may want to hold the information for longer to cary to other pages even with the same page such is username or all the rest so how to accomplsih this you may ask yourself. there are methods in php to hod the such information,one is to use database to save the info on the database,another is to use coockis save on the machine of the client,using PHP’s session mechanism,or use the GET and POST methods to pass information.these that I mentioned are the main usage of the PHP that without them PHP is such a disabled person. I will discuss each of them in more detal later on.

    voshka

    Posted in php-lesson1(variables)2 Comments

    Working with unset() function in PHP

    if you are in a state to restore a variable to its unassigned state you can use unset() Function
    Example:

    unset($assigned_var)

    this will make the $assigned_var variabel to the default value of which type it can hold no matter what assignement it has met so far.

    this function will get a variable as an argument and make the variable to default state and return nothing

    Posted in php-lesson1(variables)0 Comments

    Plese subscribe to my newsletter

    Please subscribe via your email to follow me

    E-mail:

    Subscribe
    Unsubscribe