Mysidia Adoptables Support Forum  

Home Community Mys-Script Creative Off-Topic
Go Back   Mysidia Adoptables Support Forum > Mysidia Adoptables > Tutorials and Tips

Notices

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 02-22-2011, 08:26 PM
Hall of Famer's Avatar
Hall of Famer Hall of Famer is offline
Administrator, Lead Coder
 
Join Date: Dec 2008
Location: South Brunswick
Posts: 4,448
Gender: Male
Credits: 493,433
Hall of Famer is on a distinguished road
Default Hall of Famer's PHP Tutorials: Chapter 2 - PHP Operators

Chapter II - Operators

You may wonder how we may change a value stored in a variable. A possible way to do this is to reassign a new value to this variable, which can be done easily. Another possibility is to use operators, especially Arithmetic operators. PHP has six basic types of operators, namely:

Assignment Operator: =
Arithmetic Operators: +, -, *, /, %
Concatenation Operator: .
Combined Assignment Operators: +=, -=, *=, .=, ++, --
Comparison Operators: ==, >, >=, <, <=, !=
Logic Operators: and, &&, or, ||, not, !


1. Arithmetic Operators:
We already know how the assignment operator "=" works in variable definition, so I wont spend time on it. The Arithmetic operators are used to carry out computations, an example is shown below:

PHP Code:
$num1 3;
$num2 2;
$num3 $num1 $num2;
$num4 $num1 $num2;
$num5 $num1 $num2;
$num6 $num1/$num2;
$num7 $num1 $num2;

echo 
$num1;
echo 
"</br>";
echo 
$num2;
echo 
"</br>";
echo 
$num3;
echo 
"</br>";
echo 
$num4;
echo 
"</br>";
echo 
$num5;
echo 
"</br>";
echo 
$num6;
echo 
"</br>";
echo 
$num7
The results are:

PHP Code:
3
2
5
1
6
1.5

Now you can see how operators work. Unfortunately, this is a quite amateur way of programming, which can be significantly improved with the usage of Concatenation operator.


2. Concatenation Operator:
The only one Concatenation operator is this cute little thing ., but it is rather useful. It combines two strings into one, a good example is shown below:

PHP Code:
echo "hello"."world"
The result is a single string 'helloworld' on the screen. The magic of Concatenation operator is much much more than that, we can not only play around with combination of strings, but also variables. We can easily rewrite the codes in part a as the form below:

PHP Code:
$num1 3;
$num2 2;
echo 
"The first value is ".$num1."</br>The second value is ".$num2."";
echo 
"</br>The sum is ".($num1+$num2)."</br>The difference is ".($num1 $num2)."</br>The product is ".($num1*$num2).""
You will find the following results in your screen, which significantly reduces the number of lines we write in a php file.
PHP Code:
The first value is 3
The second value is 2
The sum is 5
The difference is 1
The product is 6 
3. Combined Assignment Operators:
This type of operators may seem confusing, but they can further simplify your codes and help cut your php file size to minimum. Consider the following PHP codes below:

PHP Code:
$num 10;
$num $num +5;
echo 
$num
This works perfectly, but is not a professional way of programming. With slight modification using combined assignment operator +=, we can give our script file a much better looking:

PHP Code:
$num 10;
$num += 5;
echo 
$num
Both php codes yield the result 15 on the screen, combined assignment operators do make our codes look a lot neater. For increments of 1, the following trick can be played to further simplify our scripts:

PHP Code:
$num 5;
$num++
echo 
$num
or:

PHP Code:
$num 5;
++
$num;
echo 
$num
Both techniques will give result 6 on the screen. One may wonder what the difference is with $var++ or ++$var. Lemme use the next examples to illustrate this point:

PHP Code:
$num 3;
$num2 2*($num++);
echo 
$num2
PHP Code:
$num 3;
$num2 2*(++$num);
echo 
$num2
Interestingly, the first result is 6 while the second is 8. We can conclude that the operator causes variable $num to increment after multiplication takes place, while the second operator increments before multiplication occurs.


4. Comparison Operators:
Comparison operators are usually used in switch flows(if...else, switch...case) and loops(while, for loop). They return either true or false, as is illustrated in the example below, assume we have previous defined variable $x = 2.

PHP Code:
$x 5//true
$x == 3//false
$x >= 10//false
$x != 4//true 
A very common programming error is to misuse comparison operator == as assignment operator =. It usually will not generate any error message, which makes it even harder to detect.


5. Logic Operators:
Logic operators are quite important in switch flows and loops too in PHP programming. PHP has only four types of logic operators:

PHP Code:
and, &&: return true only if both or all statements are true
or, ||: return false only if both or all statements are false
xor: return true if one of the statements is truebut not both(applies to two statements only)
!: 
the 'not' operator 
The only difference between and/&&, or/|| is when it comes to operator precedence. and/or are at lower precedence than arithmetic operators +, -, *, /, while &&/|| execute before arithmetic operators if parenthesis() is not used. It may sound confusing at this moment, I will demonstrate this point in more details in future tutorials.

To see how logic operators work, we use the following examples below(assume we've previously defined $x=2):

PHP Code:
$x <or $x>3
$x >= and $x<=1.5;
$x == xor $x 7
The first line returns true since or operator is true so long as one of the statement. The second line returns false since and operator is true only if both statements return true. The third line seems a little bit tricky though. Both statements($x == 2, $x < 7) are true, but the entire line returns false due to the nature of xor operator.


6. Operator Precedences:
All of you have learned math in elementary school, so you must be familiar with the idea of precedences. Without using parenthesis, the operators * and / will be executed before + and -. We say that multiplication has higher order precedence than summation. For PHP operators, a list of operator ranked by precedences is shown below:

PHP Code:
++, --, ()
^
*, /, %
<, <=, >, >=
==, !=
&&
||
=, +=, -=, *=, /=, .=
and
xor
or 
Looks complicated, isnt it? I wouldnt bother myself with memorizing this table or operator precedences. Instead, I'd rather simply enclose statements in parenthesis() to avoid confusion. Good examples are:

PHP Code:
$num 2;
$result = (4+5)*$num;
echo 
$result
This gives you a result of 18, as we have expected.


Guess I am done with the tutorial of PHP operators, hope you all have at least understood the basic concept of PHP programming. What you have learned in this chapter will keep recurring in future tutorials, especially comparison and logic operators which are well-integrated in switch flows and loops.

Hall of Famer
__________________


Mysidia Adoptables, a free and ever-improving script for aspiring adoptables/pets site.
Reply With Quote
 


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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Hall of Famer's PHP Tutorials: Chapter 6 - PHP Arrays Hall of Famer Tutorials and Tips 0 03-16-2013 08:14 PM
Hall of Famer's PHP Tutorials: Chapter 5 - PHP Function Basics Hall of Famer Tutorials and Tips 0 11-23-2012 02:05 AM
Hall of Famer's PHP Tutorials: Chapter 1 - PHP syntax Hall of Famer Tutorials and Tips 10 02-02-2012 11:31 AM
Hall of Famer's PHP Tutorials: Chapter 4 - PHP Iteration Hall of Famer Tutorials and Tips 2 02-26-2011 10:48 AM
Hall of Famer's PHP Tutorials: Chapter 3 - PHP Conditional Statements Hall of Famer Tutorials and Tips 0 02-23-2011 06:59 PM


All times are GMT -5. The time now is 10:33 PM.

Currently Active Users: 1155 (0 members and 1155 guests)
Threads: 4,081, Posts: 32,032, Members: 2,016
Welcome to our newest members, jolob.
BETA





What's New?

What's Hot?

What's Popular?


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
vBCommerce I v2.0.0 Gold ©2010, PixelFX Studios
vBCredits I v2.0.0 Gold ©2010, PixelFX Studios
Emoticons by darkmoon3636