Thursday, 28 December 2017

Array can have any size and contain any sort of value. No threat of going past exhibit limits. PHP arrays are associative arrays which permit component to be put away in connection to a key value instead of a strict straight record arrange.

$capitals["name"] = "John";
$capitals["AR"] = "Little Rock";

Initialize an array:
$colors = array("red", "green", "blue");

print("The 2nd color is $colors[1]."); // prints green

$capitals = array("name" => "John", "AR" => "Little Rock");

print("$capitals[name]"); // prints John, no quotes around key inside ""

E. Print contents of an array for debugging:
print_r($colors);

Produces:
Array
(
[0] => red
[1] => green
[2] => blue
)

print_r($capitals);
produces:
Array
(
[name] => John
[AR] => Little Rock
)

Pull values out of an array:
 
$colors = array("red", "green", "blue");
list($c1, $c2) = $colors;

print("$c1 and $c2"); // prints "red and green"

 Delete from an array:

unset($colors[1]); // $colors now contains red and blue at indexes 0 and 2.


Summary of all array functions in the PHP core: http://www.php.net/manual/en/ref.array.php

PHP for circles execute a piece of code a predefined number of times. The PHP for Loop. The for circle is utilized when you know ahead of time how frequently the content should run.

Looping structures

1. while ($n < 10) {
print("$n ");
$n++;
}

2. do {
print("$n ");
$n++;
}
while ($n < 10);

3. for ($n = 1; $n < 10; $n++)
print("$n ");

4. foreach ($myarray as $item)
print("$item ");
Howdy my kin, today subject is interesting like no other php put forth basic with if expression, without much talk,  if statement are Control Structures in PHP usein making decisions.

if statement control block

if(condition)
{
statement
}

for example

1. if ($x > 0)
{

$y = 5;

}

Code explained: if $x as a value is greater than 0 as a value then assign 5 to y variable

Now, let take a look at another sample code to broaden our understanding more on this context.

2. if($username==$logusername)
{
    echo "Username Correct!";
}
or
if($access==0)
{
    echo "Access denied!";
}

    This time we will be dealing with if else statement

3. if ($a)
{
// print if $a is true or not
print($b);
$b++;
}
else
print($c);


4. if ($a > $b)
print "a is greater than b";
elseif ($a == $b) // use "elseif" or "else if"
print "a is equal to b";
else
print "a is less than b";

5. switch ($vehicle_type) { // works for integers, floats, or strings
case "car":
$car++;
break;

case "truck":
$truck++;
 break;

case "suv":
$suv++;
break;

default:
$other++;
}

We are going to talk more on switch later on...

On this instructional exercises we will find out what are Operators is all about, we use operators for assignment, Arithmetic, Comparison, Logical statement.

Assignment Operators Includes
1. = += -= /= *= %= ++ -- : unlike other programming languages.
2. .= - joining words operator.

for exmple

$text="This is a text";
$increment+=2;// increment by 2;
$decre-=1; decreament by 1;
$num1/=5; // the value of num1 divide by 5
$num2*=10; //the value of num2 multiplyby 10
$num3++; // auto increment by 1 at every run
$num3--; // auto decrement by 1 at every run

2. .= : joining words operator.

$string1.=' php world!'; // the space there matters a lot


Arithmetic Operators Includes
1. + - * / % - unlike other programming languages.
simply use for for calculation purpose

$result=$val1 + $val2; // add the value of 1 and 2
$result=$val1 - $val2; // subtract the value of 1 from 2
$result=$val1 * $val2; // add the value of 1 and 2
$result=$val1 / $val2; // divide the value of 1 from 2
$result=$val1%; // get the percentage the value of 1

Comparison Operators Includes
Is use to compare and contrast between two values, also can be use for decission making process, like if, else if, switch etc
1. == != < > <= >= : Unlike other programming languages. Even less than < and greater than > is thesame as !=.

for exmaple
1. if($val==$val2)
{
    print 'result';
}

2.  if($val!=$val2)
{
    print 'result';
}
Note: the exclamation sign ! means is not equal to.

3.  if($val < $val2) // less than
{
    print 'result';
}

4.  if($val > $val2) // greater than
{
    print 'result';
}

5.  if($val <= $val2) // less than or equal to, two condition are pased here.
{
    print 'result';
}

6.  if($val >= $val2) // greater than or equal to, two condition are also pased here.
{
    print 'answer';
}



2. === : this type is used to valid datatype and value if datatypes are thesame.
for example
$val=10;
$val2=9;
if($val===$val2)
{
    print 'Condition Passed';
}
else
{
    print 'The value '.$val.' is not equal to '.$val2;
}

3. !== - this other is the case, is also use thesame way but this time is not thesame datatype because of ! sign.

if($val!==$val2) // like the above example using ! sign you know what that means, is not equal
{
        print 'The value '.$val.' is not equal to '.$val2;
}
else
{
        print 'Condition Passed';
}

for exmaple
if($val==$val2)

Logical Operators Includes
As the name says logic

1. && || ! : Unlike other programming languages (&& and || short-form)
2. and or : like && and || but this || have lower precedence than &&

For Example
1. if($a==3 && $b=6)
{
    print 'All the condition are passed!';
}
or
if($a==3 AND $b=6)
{
    print 'All the condition are passed!';
}

OR STATEMENT
if($a==3 || $b=6)
{
    print 'One of the condition is passed!';
}

if($a==3 OR $b=6)
{
    print 'One of the condition is passed!';
}