Home Community Mys-Script Creative Off-Topic |
|
|
Thread Tools | Display Modes |
#1
|
||||
|
||||
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:
PHP Code:
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:
PHP Code:
PHP Code:
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:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
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:
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:
To see how logic operators work, we use the following examples below(assume we've previously defined $x=2): PHP Code:
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:
PHP Code:
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. |
|
|
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 |
What's New? |
What's Hot? |
What's Popular? |