Sunday, December 30, 2012

PHP Tutorial (Faq's) part-4

PHP For Loops

PHP for loops allow you to execute the same piece of code for a specified number of times. Once the specified number has been reached, the program will break out of the loop and continue processing the rest of the page.

Syntax

A PHP for loop requires 3 parameters. The 1st parameter sets a counter, the 2nd parameter defines how high the counter should get before breaking from the loop, the 3rd parameter is used to increment the counter.
Code

Example

We can use the same topic we used in the previous lesson on "while" loops. In fact, for loops are more suited to this type of usage than while loops.
Code

The above code results in:
The share price is $1. Don't sell yet.
The share price is $2. Don't sell yet.
The share price is $3. Don't sell yet.
The share price is $4. Don't sell yet.
The share price is $5. Don't sell yet.
The share price is $6. Don't sell yet.
The share price is $7. Don't sell yet.
The share price is $8. Don't sell yet.
The share price is $9. Don't sell yet.
The share price is $10. Don't sell yet.
The share price is $11. SELL NOW!

For/Each Loops

A for/each loop allows you to loop through arrays. It will execute code continuously until it has looped through every item in the array. Once it has done that, it will continue on processing the rest of the page.
Code

The above code results in:
Apples
Strawberries
Blackberries

 PHP Operators

PHP operators are characters (or sets of characters) that perform a special operation within the PHP code. For example, when you use the equals sign ( = ) to assign a value to a variable, you are using an assignment operator. When you add two numbers together using a plus sign ( + ), you are using an arithmetic operator.
Here's a list of the various PHP operators:

Artithmetic Operators

OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulus (remainder of a division)
++Increment
--Decrement

Assignment Operator

OperatorDescription
=Assign
+=Increments, then assigns
+=Decrements, then assigns
*=Multiplies, then assigns
+=Increments, then assigns
/=Divides, then assigns
%=Modulus, then assigns

Comparison Operators

OperatorDescription
==Is equal to
!=Is not equal to
>Greater than
>=Greater than or equal to
<Less than
<=Less than or equal to

Logical Operators

OperatorDescription
&&And operator. Performs a logical conjunction on two expressions (if both expressions evaluate to True, result is True. If either expression evaluates to False, result is False)
||Or operator. Performs a logical disjunction on two expressions (if either or both expressions evaluate to True, result is True).
!Not operator. Performs logical negation on an expression.

Concatenation Operators

OperatorDescription
.Concatenate (join two strings together)

 PHP Functions

Sometimes you might find yourself writing the same piece of code over and over again. You might even find yourself "copy/pasting" code so that you can re-use it in another part of your application. The problem with doing this is, you now have more code to maintain. For example, if you want to change the code, you need to change it in many different places. A better idea would be to put that code into a function.
PHP functions are self contained blocks of code that perform a specified "function". A function often accepts one or more "parameters" (also referred to as "arguments") which you can "pass" to it. By providing a different parameter to the function, you get a different result (depending on what the function actually does).

Creating PHP Functions

To create a PHP function, follow these steps:
  1. Think of a name for the function. Make it short but descriptive. It should describe what the function actually does.
  2. Type function
  3. On the same line, type the name of the function followed by opening/closing brackets ( eg, myFunction() ).
  4. Add an opening curly brace ( { )
  5. Type the code that makes up the function
  6. Finish with a closing curly brace ( } )

Example

Code

Calling PHP Functions

The function doesn't actually do anything until you call it. To call a function, do the following:
  1. Write the function name followed by opening and closing brackets (i.e. functionName() )
  2. Between the opening/closing brackets, provide any parameters the function requires. If it doesn't require any parameters you can leave it blank.

Example

In this example, we create the same function we created in the previous example. We then call that function:
Code

The above code results in the following:
My favorite fruit is pomegranate.

Passing Parameters

The previous example is a very simplistic version of a function. We could improve this function by allowing it to accept a parameter. For example, imagine if, at the time we called the function, we could pass it the name of a fruit. The function could then display the name of that fruit (instead of displaying "pomegranate" every time).

Example 1

Code

The above code results in the following:
My favorite fruit is: Watermelon
This is one of the things that make functions so cool. For example, by accepting a parameter (such as the fruit name), we could let our website users provide us with the name of their favorite fruit. Behind the scenes, we could put that fruit into a variable, then pass that variable to our function. The function could then display the name of their favorite fruit (instead of "pomegranate"!).

Example 2

In this example, the fruit name is supplied as a variable. For the purposes of this tutorial, we set the variable to a hard-coded string, but there's no reason this couldn't be a value provided by the user (for example, a value from a form).
Code

The above code results in the following:
My favorite fruit is: Watermelon

Return Values

In the previous examples, our functions simply displayed a string to the user. Sometimes, you might not want your function to automatically display the result. Sometimes you might just want the function to return the result to you so that you can use it for your own programmatical purposes. That way, it's your choice whether you display it to the user, or do something else with the result.
To create a return value, you type return followed by the value you want to return.

Example

Code

The above code results in the following:
104

No comments:

Post a Comment