IT Community - Software Programming, Web Development and Technical Support

Operations in PHP

This is a discussion on Operations in PHP within the PHP Programming forums, part of the Web Development category; Hi, Once you have information stored in a variable, what are you going to do with it? As we have ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 04-18-2008, 02:08 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Operations in PHP

Hi,

Once you have information stored in a variable, what are you going to do with it? As we have seen, you can echo it to a web page. You also can manipulate the information with operators. Here are examples of mathematical operations in PHP:


$b = $a + 3;
$c = $a - 5;
$b = $b * 5;
$d = $b / $c;
__________________
Regards,
Senraj.A

Last edited by senraj : 04-18-2008 at 03:16 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-18-2008, 02:16 AM
Falcon Falcon is offline
D-Web Analyst
 
Join Date: Nov 2007
Location: Chennai
Posts: 288
Falcon is on a distinguished road
Default Re: Operations in PHP

Hi

In addition to the basic assignment operator (=), there is a string-concatenation operator (.=) and combination assignment/mathematical operators. Here are some examples:
PHP Code:
$string1 "super";
$string2 "star";
$string1 .= $string2// $string 1 now equals "superstar"

$name "Stephen";
$name .= " Crampton"// note the space; name now equals "Stephen Crampton"

$c 5;
$c += 1// result is $c + 2, or 6

$c -= -1// result is $c - (- 1), or 7

$x 5;
$x *= $c// result is $x * $c, or 35

$y 70;
$y /= $x // result is $y / $x, or 2 
Regards
Falcon

Last edited by Falcon : 04-18-2008 at 03:20 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-18-2008, 03:19 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Split an array into chunks

To split an array into smaller chunks or smaller sized arrays, we use array_chunk() function of PHP. This will return arrays of several smaller sizes and each array's index number will start with zero unless you want to use the preserve_keys parameter to preserve the original index numbers from the input array used. The syntax is:
array_chunk ( array input, int size [, bool preserve_keys] )

Using the above function in an example:

PHP Code:
<?
$my_array 
= array("One""Two""Three""Four");
$split_array array_chunk($my_array2);
print_r($split_array);
//Output: Array ( 
//                [0] => Array ( 
//                              [0] => One 
//                              [1] => Two ) 
//                [1] => Array ( 
//                              [0] => Three 
//                              [1] => Four ) 
//                               ) 
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-18-2008, 03:19 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Operations in PHP

Hi,,
There also are simple increment/decrement operators, which are essentially shorthand ways of adding or subtracting one from a number:

PHP Code:
$a 5;
$a++; // $a has been incremented by 1, and now equals 6

$a--; // $a has been decremented by 1, and now equals 5

$b $a++; 
// Note that the incrementing happens after the evaluation:
// First, $b is calculated to be 25. Then, $a is
// incremented to become 6
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 04-18-2008, 03:21 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Combine an array with data elements and other with its keys

We can create an array by combining one array with keys and second array with corresponding data elements. Note that the number of keys and data elements in both the arrays has to be equal for this operation to be successful. We will make use of built-in function array_combine(). Its syntax is:
array_combine ( array keys, array values )

and the example using array_combine() is:
PHP Code:
<?
$keys_array 
= array(1,2,3,4);
$data_array = array("one","two","three","four");
$new_array array_combine($keys_array$data_array);
print_r($new_array);
//Output: Array
// (
//     [1]  => one
//     [2]  => two
//     [3]  => three
//     [4]  => four
// )
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 04-18-2008, 03:22 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Merging two or more arrays

Using an built-in function array_merge() we can merge two or more arrays to form a single array. The values from the second array are appended at the end of first array and so on. So, if three arrays are to be merged, elements from third will be appended at the end of second array and then it will be appended at the end of the first array. Take a look at this syntax and example:
array_merge ( array array1 [, array array2 [, array ...]] )

PHP Code:
<?
$first_array 
= array(1,2);
$second_array = array(3,4);
$third_array = array(5,6);

$merged_array array_merge($first_array,$second_array,$third_array);
print_r($merged_array);
//Output: Array 
//          ( [0] => 1 
//            [1] => 2 
//            [2] => 3 
//            [3] => 4 
//            [4] => 5 
//            [5] => 6 
//          ) 
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 04-18-2008, 03:23 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Operations in PHP

Hi,

If you want $a to be incremented first, put the ++ in front of it
as follows:

PHP Code:
$b * ++$a// $a incremented to 7; $b becomes 35

$c 100;
$c -= ++$b $a++; 
// $b becomes 36; $c is calculated to be
// 100 - ( 36 + 7 ); finally, $a becomes 8
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 04-18-2008, 03:24 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Searching an array

Searching for a value in an array is made simple using the function array_search(). If the keyword is found in the array, then the corresponding key of that value is retured for further operations.

PHP Code:
<?
$months 
= array(1=>"Jan""Feb""Mar""Apr""May""June""July""Aug",
"Sep""Oct""Nov""Dec");
$key array_search("May"$months); //Searching for May in months array
echo $key;
// We can also print the value that key points at
echo $months[$key];
?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 04-18-2008, 03:25 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Operations in PHP

Hi,



Control statements are fundamental to programming languages. Control statements permit you to take some action if something is the case, and different action (or no action) otherwise. There are three common control statements in PHP:

if . . . else: As the words suggest, this statement tests if something is true. If it is, then it does one thing, else it does another thing. The else part can be omitted. For example,

PHP Code:
      $IP_address getenv("REMOTE_ADDR");
      if ( 
$IP_address == "121.114.221.111" ) {
        echo 
"Hello, George.  Welcome to my web site.");
      }
      else {
        echo 
"Howdy, stranger.";
      }

      
$temperature 71;
      if ( 
$temperature >= 50 ) {
        echo 
"Nice day!";
      } 
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 04-18-2008, 03:26 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Operations in PHP

Hi,

while . . . do: This statement keeps repeating specified statements again and again, so long as a particular condition is true. The statements that are repeated should somehow affect the condition. Otherwise, the statement may never become false and the program will go into an infinite loop. Here is an example of a while loop:

PHP Code:
$a 2;
while ( 
$a 100 
{
  echo 
" $a";
  
$a *= $a;
}

$b 1;
while ( 
$b 10 
{
  
$a++;
// OOPS!  This is an infinite loop! 
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 04-18-2008, 03:28 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: Operations in PHP

Hi,

or . . . do: This is basically shorthand for a while loop where the condition is whether a particular variable has yet to reach a certain value. Each time the loop is executed the variable is generally incremented or decremented. This kind of control statement is useful when you want to count through a series of numbers or objects, or repeat a set of statements of particular number of times. For example,


PHP Code:
$sum 0;
for ( 
$a 1$a <= 10$a++ ) 
{
  
$sum += $a;
}

echo 
"Sum of numbers from 0 to 10 equals $sum.";

// The for loop is just shorthand for the following while loop:

$a 1;
while ( 
$a <= 10 
{
  
$sum += $a;
  
$a++;

__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 04-18-2008, 05:07 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

PHP File Operations

The input and output in a web application usually flow between browser, server, and database, but there are many circumstances in which files are involved too. Files are useful for retrieving remote web pages for local processing, storing data without a database, and saving information that other programs need to access. Learn how to: handle file uploads, restrict the size and type of uploads, prevent files from being overwritten, organize uploads into specific folders, handle multiple uploads and more.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 04-21-2008, 12:54 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

The precedence of an operator specifies how "tightly" it binds two expressions together. For example, in the expression 1 + 5 * 3, the answer is 16 and not 18 because the multiplication ("*") operator has a higher precedence than the addition ("+") operator. Parentheses may be used to force precedence, if necessary. For instance: (1 + 5) * 3 evaluates to 18. If operator precedence is equal, left to right associativity is used.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14 (permalink)  
Old 04-21-2008, 12:55 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

An operator is something that you feed with one or more values (or expressions, in programming jargon) which yields another value (so that the construction itself becomes an expression). So you can think of functions or constructions that return a value (like print) as operators and those that return nothing (like echo) as any other thing.

There are three types of operators. Firstly there is the unary operator which operates on only one value, for example ! (the negation operator) or ++ (the increment operator). The second group are termed binary operators; this group contains most of the operators that PHP supports, and a list follows below in the section Operator Precedence.

The third group is the ternary operator: ?:. It should be used to select between two expressions depending on a third one, rather than to select two sentences or paths of execution. Surrounding ternary expressions with parentheses is a very good idea.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15 (permalink)  
Old 04-21-2008, 01:05 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Arithmetic operations

-$a (Negation) - Opposite of $a.
$a + $b (Addition) - Sum of $a and $b.
$a - $b (Subtraction) - Difference of $a and $b.
$a * $b (Multiplication) - Product of $a and $b.
$a / $b (Division) - Quotient of $a and $b.
$a % $b (Modulus) - Remainder of $a divided by $b.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #16 (permalink)  
Old 04-21-2008, 01:05 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

The division operator ("/") returns a float value anytime, even if the two operands are integers (or strings that get converted to integers).
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #17 (permalink)  
Old 04-21-2008, 01:07 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the rights (that is, "gets set to").

The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:

for e.g
PHP Code:
<?php

$a 
= ($b 4) + 5// $a is equal to 9 now, and $b has been set to 4.

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #18 (permalink)  
Old 04-21-2008, 01:08 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:

PHP Code:
<?php

$a 
3;
$a += 5// sets $a to 8, as if we had said: $a = $a + 5;
$b "Hello ";
$b .= "There!"// sets $b to "Hello There!", just like $b = $b . "There!";

?>
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #19 (permalink)  
Old 04-21-2008, 01:08 AM
Jeyaseelansarc Jeyaseelansarc is offline
D-Web Genius
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,158
Jeyaseelansarc is on a distinguished road
Send a message via AIM to Jeyaseelansarc
Default Re: Operations in PHP

The assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. Since PHP 4, assignment by reference has been supported, using the $var = &$othervar; syntax, but this is not possible in PHP 3. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere.
__________________
With,
J. Jeyaseelan

Everything Possible
Add Post to del.icio.us