SEO: Download Free software, Free Engineering Notes, The Infinite Info

What's New Here?


Conditional Logic is all about asking "What happens If ... ". When you press a button labelled "Don't Press this Button - Under any circumstance!" you are using Conditional Logic. You are asking, "Well, what happens If I do press the button?"

Conditional Logic uses the "If" word a lot. For the most part, you use Conditional Logic to test what is inside of a variable. You can then makes decisions based on what is inside of the variable.
In PHP, you use the "If" word like this:
if($username == "authentic") {
       //Code to let user access the site here;
}
The different conditional logics are:
  • if statement
  • else statement
  • else-if statement
  • switch statement
  • ternary statement
If statement
The if statement tests if a certain condition is true or false and acts upon it accordingly.
Syntax:
if(condition) { 
      perform this action;
}
If the condition in the parenthesis is true, then the code following the condition is executed, otherwise it will not.
Example:<?php 
    $number = 10;
    if ($number == 10) {
         echo "Number is equal to 10";
    }
?>
else statement
If the condition in the parenthesis in an if statement is true, then the code following the condition will be executed, otherwise the else statement is executed. The else statement works in conjunction with the if statement and executes certain code if the condition in the if statement is false.
Syntax:if (condition) { 
    perform action 
} else {
    perform action if above condition is false
}
Example:<?php
    $number = 5;
    if($number == 10) {
        echo "Number is equal to 10";
    } else {
        echo "Number is not equal to 10";
    }t
?
else-if statement
The if statement tests a single condition and performs an action if the condition is true and the else statement performs and action if the condition in the if statement is false, but if there are more than two possibilities then else-if statement is executed. The else-if statement is used in conjunction with if statement. Unlike the else statement, it does not specifically perform a certain action if the condition in the if statement  is false, but rather it performs an action if the condition in the if statement is another specific value specified in the else if statement itself.
Syntax:
if(condition){
    perform this action
} else if(another condition){
    perform this action
}
Example:
<?php 
    $number = 5;
    if($number == 10) {
        echo "Number is equal to 10";
    } else if ($number == 5) {
        echo "Number is equal to 5";
    }
?>
Switch statement
The switch statement is  specifically designed for comparing one variable to a number of possible values. It can be used as a substitute of if, else-if, else  structure. There is an important keyword break keyword. The break keyword is used to make sure that the switch statement will not fall through to the next possible value, even if the value is incorrect within the switch statement.
Syntax:
switch(variable){
    case possible value:
        perform this action;
        break;
    case possible value:
        perform this action;
        break;
    case possible value:
        perform this action;
        break;
    default:
        perform this action if none of the value matches;
}
Example:<?php 
    $number = 2;
    switch($number) {
        case 1:
            echo "Number is equal to 1";
            break;
        case 2:
            echo "Number is equal to 2";
            break;
        case 3:
            echo "Number is equal to 3";
            break;
        default:
            echo "Number does not matches the value";
    }
?>
The ternary operator re
The ternary operator is the question mark symbol(?) it works the same way as the if-else structure.
Syntax:
variable = (condition)?value the variable will take if condition is true: value of variable will take if condition is false
Example:<?php 
    $x;
    $y = 5;
    $x = ($y == 3)?6:12;
    echo "x=".$x;
?>
VARIABLES:
Variables represent data. For example,
The variable “city” holds the literal value “Kathmandu” when appearing in your script as: $city = “Kathmandu”;

Variables in PHP are represented by a dollar sign followed by the name of the variable.
PHP supports the basic data types of strings, integers, double precision floating point numbers, etc. Variables are not tied to a specific data type, and may take any of the data types.


OPERATORS:
Using different types of operators we values are assigned to variables. Different types of operators are as follows:
a. Arithmetic operator:
+, -, *, /, and % are assignment operators.
b. Assignment operator:
The basic assignment operator is (=) sign.
c. Comparison operator:
This operator is used to compare to values. Most of the comparison operators are:
== equal to
!= not equal to
> greater than
< less than
>= greater than or equal to
<= less than or equal to
d. Logical operator:
This operator allows your script to determine the status of the conditions. Mostly this operator is used in if.... else and while control statements.
Following are the logical operators:
! -> not
Example: !$x. This means TRUE if $x is not true.
&& -> and
Example: $x && $y. This means TRUE if $x and $y are true.
|| -> or
Example: $x || $y. This means TRUE if either $x or $y is true.

e. Increment or decrement operator:
This operator adds or subtracts from a variable.
Pre-increment: ++$x
It means increment by 1 and returns $x
Post-increment: $x++
It means return $x and then increment $x by 1
Pre-decrement: --$x
It means decrement by 1 and returns $x
Post-decrement: $x--
It means return $x and then decrement $x by 1
PHP (recursive acronym for "PHP: Hypertext Preprocessor").
PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994. Now it has version 4.0.3 with numerous improvements and refinements over the original release.
PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.


Common uses of PHP:

It performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
It can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.
You can add, delete, modify elements within your database thru PHP.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your website.
It can encrypt data.

Apple Users:

If you have OS X, then try these sites to get up and running with PHP:



What you're doing here is getting the apache server up and running, so that you can run PHP scripts offline. Pay particular attention to where files are stored, and to the "localhost" address.

Linux Users

There are quite a few sites out there to help Linux users get up and running with the Apache server and PHP. Here are three sites that are worth checking out:




If you know any better ones, we'd be interested in hearing from you!

Windows Users:


PHP is embedded in HTML. (You could have a file that contains
almost no HTML, but usually it's a mixture.) That means that in amongst your normal
HTML (or XHTML if you're cutting-edge) you'll have PHP statements like this:
<body >
<strong>How to say "Hello, World!"</strong>
<?php echo "Hello, World!";?>
<br>
</body>

NEXT TOPIC >>
2012 LEARN PHP BASICS. All Rights Reserved.
Designed by Theme Junkie, Converted by Uong Jowo