Showing posts with label php-basics. Show all posts
Showing posts with label php-basics. Show all posts

Thursday, 28 December 2017

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!';
}
Hello! friends today exercise will be interesting as we are going to work on the fun part of php function, this will let us to declare and use function.

how to create your own php function, see code

function functionName(parameters)
{
    statement;
}

simple right? sure

Now let create one and test it, open your code editor and copy the below code to your editor, save as function.php and run your localhost see result

function helloWorld()
{
    $text="Hi! Hello World!";

    return $text;
}

we have just created our php helloWorld function, this will not output anything untill we call that function, here is how

echo helloWorld();

This will now output the hello World message to the user
Other examples

function Add($num1,$num2)
{
    $result=$num1+$num2;

    return $result;
}

echo Add(10,23);

or

$num1=10;
$num2=23;
echo Add($num1,$num2);

if you don't understand about variable and datatype click here
Hello guys today we are going to work with php variable and data types the fun aspect of PHP, what you need is and editor and php engine(Apache Server) to power our script to run.

To declare a variable in php is really simple, use the dollar sign($) followed by a name of your choice that relate to your context, for example

$storeName="Your name";

Because i want t store string/letter, make sure you use double quote when you are dealing with string or letter.

Always follow variable naming principle, use underscore, don't start with number, or hyphen or any special character such as this @#$%^&*()-=+

Example Two:

To store integer or float number see sample code
$MyNumber=23;
$myFloat=299.00;

We are going to deal with function later on, that will bring us to - local scope

If you don't want your code to get error simply use semi-colon to terminate after everything, this tell the system that this line of code has done it job.