IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesPerlOperators  Print This     

Cross Ref > Operators

By Mike Prestwood

Perl versus PHP: A side by side comparison between Perl and PHP.

 
Operators
 

A language symbol used for assignment, comparison, computational, or as a logical.

Assignment

[Other Languages] 

Languages Focus

Common assignment operators for languages include =, ==, and :=. An assignment operator allows you to assign a value to a variable. The value can be a literal value like "Mike" or 42 or the value stored in another variable or returned by a function.

Perl:   =

Perl assignment operators:

= Assignment $x = 8;
+= Addition $x += 8;
-= Substraction $x -= 8;
*= Muliplication $x *= 8;
/= Division $x /= 8;
%= Modulus $x %= 8;
**= Exponent $x **= 8;

Syntax Example:
$FullName = "Randy Spitz";
$Age = 38;
PHP:   =

PHP uses = for it's assignment operator.

Syntax Example:
$fullname = "Randy Spitz";
$age = 38;




Comparison Operators

[Other Languages] 

General Info: Round Floating Point Numbers

When comparing floating point numbers, make sure you round to an acceptable level of rounding for the type of application you are using.

Languages Focus

A comparison operator compares two values either literals as in "Hello" and 3 or variables as in X and Counter. Most languages use the same operators for comparing both numbers and strings. Perl, for example, uses separate sets of comparison operators for numbers and strings.

Perl:   ==, !=

Common comparison operators:

== equal
!= not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

Perl also offers additional string comparison operators:

eq string equals
ne string not equal
lt string less than
gt string greater than
le string less than or equal
ge string greater than or equal

Syntax Example:
#Does Perl evaluate the math correctly? No!
if ((.1 + .1 + .1) == .3) {
print("Correct<br>");
} else {
print("Not correct<br>");
}
PHP:   ==, != or <>

Common comparison operators:

== equal
!= or <> not equal
< less than
> greater than
<= less than or equal
>= greater than or equal

PHP 4 and above also offers === for indentical (equal plus same type) and !== for not identical (not equal or not same type).

Syntax Example:
//Does PHP evaluate the math correctly? No!
if (.1 + .1 + .1 == .3) {
echo "correct";
}
else {
echo "not correct";
}




Logical Operators

[Other Languages] 

Languages Focus

Logical operators perform conditional and, or, and not operations. Some languages support both binary logical operators that link two and unary logical operators negate (make opposite) the truth value of its argument. Finally, some languages short circuit logic. For example, with this or that, if this is an expression returning true, then that is never executed.

Perl: 

Perl logical operators:

&& or and and, as in this and that
|| or or or, as in this or that
! Not, as in Not This

Syntax Example:
#Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  #Do something.
}
PHP:   and, &&, or, ||, !, Xor

PHP logical operators:

and, && and, as in this and that
or, || or, as in this or that
! Not, as in Not This
Xor either or, as in this or that but not both

Syntax Example:
#Given expressions a, b, c, and d:
if !((a && b) && (c || d)) {
  #Do something.
};




String Concatenation

[Other Languages] 
Perl:  "String Concatenation" .

Perl uses a period (.) known as a dot to concatenate strings.

Syntax Example:
$fname = "Mike";
$lname = "Prestwood";

$fullname = $fname . $lname . "
";

print "My name is " . "Mike.
";
PHP:  "String Concatenation" .

PHP uses a period (.) known as a dot to concatenate strings.

Syntax Example:
$fname = "Mike";
$lname = "Prestwood";

$fullname = $fname . $lname . "
";

echo "My name is " . "Mike.
";




Unary Operators

[Other Languages] 

General Info: Unary Operator

An operation with only one operand (a single input). Common unary operators include + plus, - minus, and bitwise not. Some operators can function as both unary and binary operators. For example, + and - operators can serve as either.

Languages Focus

What unary operators are supported in additoin to the standard plus, minus, and bitwise not.

Perl: 

An operation with only one operand (a single input). The following are the Perl unary operators: !, -, ~, +, \, &, and *.

  • ! performs logical negation which is "not"
  • - performs arithmetic negation if the operand is numeric.
  • ~ performs bitwise negation, that is 1's complement.
  • + has no semantic effect whatsoever, even on strings.
  • \ creates a reference to whatsoever follows.
  • & Address of operator.
  • * Dereference address operator.
PHP: 

A unary operator operates on only one value.

PHP Examples:

  • ! negation operator
  • ++ increment operator
  • -- decrement operator




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2024 Prestwood IT Solutions.   [Security & Privacy]