Employing PHP operators
function make_thumb($img_name,$filename,$new_w,$new_h)
{
//get image extension.
$ext=getExtension($img_name);
//creates the new image using the appropriate function from gd library
if (!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
{
$src_img = imagecreatefromjpeg($img_name);
}
if (!strcmp("png",$ext))
{
$src_img = imagecreatefrompng($img_name);
}
if (!strcmp("gif",$ext))
{
$src_img = imagecreatefromgif($img_name);
}
PHP operators are symbols and sometimes text that we can use to manipulate variables and their respective values. Using the operator in this way is called using operators in operations.
The item that the operator acts upon is called the operand. There are several types of operators in PHP below is a listing of all of the specific types of them and examples.
- String Operators
- Arithmetic Operators
- Assignment Operators
- Combined Operators
- Reference Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Bonus Operators (bonus meaning "more")
String Operators
Operators that act upon strings
Usually referred to concatenation operators. This type of operator is used to connect two strings together.
- . (period) like connecting strings
- .= (period equal) like addition for strings
Arithmetic Operators
Operators that act upon integers
These operators are used in conditional statements acting upon integers.
- + (addition)
- - (subtraction)
- * (multiplication)
- / (division)
- % (modulus)
Assignment Operators
Operators that assign
Assignment operators assign something to something else, like a value to a variable.
- = (equal)
Combined Operators
Operators that use two operators to do one job
These operators are used as shorthand for doing more complex operations.
- += ($a = $a + $b)
- -= ($a = $a - $b)
- *= ($a = $a * $b)
- /= ($a = $a / $b)
- %= ($a = $a % $b)
- .= ($a = $a . $b)
Reference Operators
Operators that create a reference from one item to another
- &
Basically, $b = &$a means, that $b references the value of $a from this point forward in your script, so if $a changes below your reference, then the value of the referenced variable changes with the new assignment of $a as long as it happens below the referenced call in your script.
There is expanded explanation in the, "Avoid Replication with the PHP Reference Operator" post
Comparison Operators
Operators that compare two or more things
You can use these operators to compare two values.
- == (equivalent too)
- === (identical, including data type)
- != (NOT equivalent too)
- <> (NOT equivalent too)
- < (less than)
- > (greater than)
- >= (greater than and equivalent too)
- <= (less than and equivalent too)
- instanceof (used to determine if an instance of an object is of a specific type, inherits from a specific type, or implements a specific interface)
Logical Operators
Operators that combine the results of logical conditions
You will use these operators in conditional statements like in the IF statement
- ! (not)
- && (AND)
- ¦¦ (OR)
- AND (AND)
- OR (OR)
Bitwise Operators
Operators that treat integers as a series of bits
- & (bitwise AND)
- ¦ (bitwise OR) bits set in $ OR $b are set in the result $C
- ˜ (bitwise NOT)any BITS contained in $a are not set in the result $C
- ˆ (bitwise XOR)compares $A and $B, any bits not set in both are returned as the value of $C
- << (left shift) SHIFTS a variable $a LEFT $b bits
- >> (right shift)SHIFTS a variable $a RIGHT $b bits
bits set in $ AND $b are set in the result $C
Bonus Operators (bonus meaning "more")
A variety of uncategorized operators
- Ternary Operator
- Error Suppression Operator
- Execution Operator
Ternary Operators: act like an expression version of an IF else statement. the following in an example situation where using the Ternary Operator will be of great service to us as PHP programmers.
- ?: (question mark colon
- $a = condition ? value IF condition is true : value IF condition is false
<!--?php $sentence = $value == 1 //variable that recieves the assignment and the condition ? 'You have one egg.' // what $sentance equals if the condition is TRUE : 'You have '.$value.' eggs.'; // what $sentance equals if the condition is FALSE this is the default value ?-->
Error Suppression Control Operator: You will use this symbol to prepended to an expression in PHP, then any error messages that could be generated by said expression will be ignored by PHP and your script will continue.
- @ (AT)
<!--?php @require_once("file.php") //would negate the reason to require a file ?-->
Execution Operator: These are NOT single quotes rather the stroke combined with the tilde(˜) key on many keyboards. REMEMBER: The backtick operator is disabled when safe mode is enabled or shell_exec() is disabled. Use of the backtick operator is identical to shell_exec() function.
- `` (backticks) must use two in conjunction
- if you have a problem with your script and you cannot seem to locate it, use dreamweaver to search for the backtick, you might have fat fingered it accidentally.
<!--?php $output = shell_exec('ls -lart'); echo "$output"; //wrap in pre tags ?-->