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, Increment/decrement operators are unique in the sense that they operate only on variables and not on any value. ...


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

Register FAQ Members List Calendar Mark Forums Read
  #41 (permalink)  
Old 05-07-2008, 12:32 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #42 (permalink)  
Old 05-07-2008, 12:33 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #43 (permalink)  
Old 05-07-2008, 12:35 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #44 (permalink)  
Old 05-08-2008, 01:24 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #45 (permalink)  
Old 05-08-2008, 01:29 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #46 (permalink)  
Old 05-08-2008, 01:30 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #47 (permalink)  
Old 05-08-2008, 01:38 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #48 (permalink)  
Old 05-08-2008, 01:39 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #49 (permalink)  
Old 05-08-2008, 02:37 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #50 (permalink)  
Old 05-09-2008, 03:02 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,

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #51 (permalink)  
Old 05-09-2008, 04:17 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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #52 (permalink)  
Old 05-09-2008, 04: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,

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #53 (permalink)  
Old 05-09-2008, 04:21 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,

the previous script the following way:
PHP Code:
function func ()
{
$GLOBALS["var"] = 2;
}
$var 1;
func();
print 
$var
It prints the value 2.
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:
function func()
{
global 
$var;
$var 2;
}
$var 1;
func();
print 
$var
This way of writing the example also prints the number 2.
__________________
Regards,
Senraj.A
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #54 (permalink)  
Old 05-09-2008, 04:22 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,

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:
function get_global_variable_value($name)
{
return 
$GLOBALS[$name];
}
$num 10;
$value get_global_variable_value("num");
print 
$value
This code prints the number 10. However, making changes to $value before
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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #55 (permalink)  
Old 07-12-2008, 02:36 AM
Miakoda Miakoda is offline
D-Web Trainee
 
Join Date: Jul 2008
Posts: 49
Miakoda is on a distinguished road
Default Re: Operations in PHP

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


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


All times are GMT -7. The time now is 04:35 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0