This is a discussion on Operations in PHP within the PHP Programming forums, part of the Web Development category; Hi, Increment/decrement operators are unique in the sense that they operate only on variables and not on any value. ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi, Increment/decrement operators are unique in the sense that they operate only on variables and not on any value. The reason for this is that in addition to calculating the result value, the value of the variable itself changes as well. IncrementDecrement Operators.doc
__________________ Regards, Senraj.A |
| Sponsored Links |
| |||
| Hi, Increment/decrement operators As you can see from the previous table, there’s a difference in the value of post- and pre-increment. However, in both cases, $var is incremented by 1. The only difference is in the value to which the increment expression evaluates. Example 1: $num1 = 5; $num2 = $num1++;// post-increment, $num2 is assigned $num1's original ➥value print $num1; // this will print the value of $num1, which is now 6 print $num2; // this will print the value of $num2, which is the ➥original value of $num1, thus, 5
__________________ Regards, Senraj.A |
| |||
| Hi, Increment/decrement operators Example 2: $num1 = 5; $num2 = ++$num1;// pre-increment, $num2 is assigned $num1's ➥incremented value print $num1; // this will print the value of $num1, which is now 6 print $num2; // this will print the value of $num2, which is the ➥same as the value of $num1, thus, 6
__________________ Regards, Senraj.A |
| |||
| Hi, Incrementing Strings Strings (when not numeric) are incremented in a similar way to Perl. If the last letter is alphanumeric, it is incremented by 1. If it was ‘z’, ‘Z’, or ‘9’, it is incremented to ‘a’, ‘A’, or ‘0’ respectively, and the next alphanumeric is also incremented in the same way. If there is no next alphanumeric, one is added to the beginning of the string as ‘a’, ‘A’, and ‘1,’ respectively. If this gives you a headache, just try and play around with it. You’ll get the hang of it pretty quickly.
__________________ Regards, Senraj.A |
| |||
| Hi, The Cast Operators PHP provides a C-like way to force a type conversion of a value by using the cast operators. The operand appears on the right side of the cast operator, and its result is the converted type according to the following table. Cast Operators.doc
__________________ Regards, Senraj.A |
| |||
| Hi, The casting operators change the type of a value and not the type of a variable. For example: -------------- $str = "5"; $num = (int) $str; This results in $num being assigned the integer value of $str (5), but $str remains of type string.
__________________ Regards, Senraj.A |
| |||
| Hi, The Silence Operator The operator @ silences error messages during the evaluation process of an expression. for example: ------------- with out silence operator $obj = new sample(); $obj -> example(); with silance operator @$ob->example();
__________________ Regards, Senraj.A |
| |||
| Hi, The One and Only Ternary Operator One of the most elegant operators is the ?: (question mark) operator. Its format is truth_expr ? expr1 : expr2 The operator evaluates truth_expr and checks whether it is true. If it is, the value of the expression evaluates to the value of expr1 (expr2 is not evaluated). If it is false, the value of the expression evaluates to the value of expr2 (expr1 is not evaluated).
__________________ Regards, Senraj.A |
| |||
| Hi, Ternary Operator For example, the following code snippet checks whether $a is set (using isset()) and displays a message accordingly: $a = 99; $message = isset($a) ? '$a is set' : '$a is not set'; print $message; This example prints the following: $a is set
__________________ Regards, Senraj.A |
| |||
| Hi, FUNCTIONS A function in PHP can be built-in or user-defined; however, they are both called the same way.The general form of a function call is func(arg1,arg2,…) The number of arguments varies from one function to another. Each argument can be any valid expression, including other function calls. Here is a simple example of a predefined function: $length = strlen("John"); strlen is a standard PHP function that returns the length of a string. Therefore, $length is assigned the length of the string "John": four. Here’s an example of a function call being used as a function argument: $length = strlen(strlen("John")); You probably already guessed the result of this example. First, the inner strlen("John") is executed, which results in the integer 4. So, the code simpli- fies to $length = strlen(4); strlen() expects a string, and therefore (due to PHP’s magical autoconversion between types) converts the integer 4 to the string "4", and thus, the resulting value of $length is 1, the length of "4".
__________________ Regards, Senraj.A Last edited by senraj : 05-09-2008 at 04:16 AM. |
| |||
| Hi, User-Defined Functions The general way of defining a function is function function_name (arg1, arg2, arg3, …) { statement list } To return a value from a function, you need to make a call to return expr inside your function. This stops execution of the function and returns expr as the function’s value. The following example function accepts one argument, $x, and returns its square: function square ($x) { return $x*$x; } After defining this function, it can be used as an expression wherever you desire. For example: print 'The square of 5 is ' . square(5);
__________________ Regards, Senraj.A |
| |||
| Hi, Function Scope Every function has its own set of variables. Any variables used outside the function’s definition are not accessible from within the function by default. When a function starts, its function parameters are defined. When you use new variables inside a function, they are defined within the function only and don’t hang around after the function call ends. In the following example, the variable $var is not changed by the function call: function func () { $var = 2; } $var = 1; func(); print $var; When the function func is called, the variable $var, which is assigned 2, is only in the scope of the function and thus does not change $var outside the function. The code snippet prints out 1. Now what if you actually do want to access and/or change $var on the outside? As mentioned in the “Variables” section, you can use the built-in $GLOBALS[] array to access variables in the global scope of the script.
__________________ Regards, Senraj.A |
| |||
| Hi, the previous script the following way: PHP Code: A global keyword also enables you to declare what global variables you want to access, causing them to be imported into the function’s scope. However, using this keyword is not recommended for various reasons, such as misbehaving with assigning values by reference, not supporting unset(), and so on. Here’s a short description of it—but please, don’t use it! The syntax is global $var1, $var2, ...; Adding a global line for the previous example results in the following: PHP Code:
__________________ Regards, Senraj.A |
| |||
| Hi, Returning Values By Value You can tell from the previous example that the return statement is used to return values from functions. The return statement returns values by value, which means that a copy of the value is created and is returned to the caller of the function. For example: PHP Code: the print statement only affects $value and not the global variable $num. This is because its value was returned by the get_global_variable_value() by value and not by reference.
__________________ Regards, Senraj.A |
| |||
| Operator Description Example Result 1) + Addition x=2 x+2 answer . 4 2) - Subtraction x=2 x-5 answer. 3 3)* Multiplication x=4 x*5 answer 20 comparison operator are like ==, !=,>, >=,===, !== in php are many operator left operators i will discuss later. |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| String Operations using different databases | srikumar_l | Database Support | 0 | 12-05-2007 02:27 AM |
| Date Operations in different databases | srikumar_l | Database Support | 3 | 12-04-2007 06:59 AM |
| Calculations and other Operations on Field Values | Pvinothkumar | HTML, CSS and Javascript Coding Techniques | 28 | 10-19-2007 04:36 AM |
| How to Perform SQL Server Row-by-Row Operations Without Cursors? | oxygen | Database Support | 1 | 07-30-2007 12:22 AM |
| .NET support oneway web service operations? | devarajan.v | XML and SOAP | 1 | 07-26-2007 03:46 AM |