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 ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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. |
| Sponsored Links |
| |||
| 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: Falcon ![]() Last edited by Falcon : 04-18-2008 at 03:20 AM. |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi,, There also are simple increment/decrement operators, which are essentially shorthand ways of adding or subtracting one from a number: PHP Code: // First, $b is calculated to be 25. Then, $a is // incremented to become 6
__________________ Regards, Senraj.A |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| Hi, If you want $a to be incremented first, put the ++ in front of it as follows: PHP Code: // 100 - ( 36 + 7 ); finally, $a becomes 8
__________________ Regards, Senraj.A |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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:
__________________ Regards, Senraj.A |
| |||
| 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:
__________________ Regards, Senraj.A |
| |||
| 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:
__________________ Regards, Senraj.A |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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 |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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:
__________________ With, J. Jeyaseelan Everything Possible |
| |||
| 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 |