ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

A Server Side Web Tutorial Series # 3 - PHP Operators; Arithmetic, Array, Assignment, Comparison, Logical & more!

Updated on May 10, 2014

The Six Types of Operators in PHP

The six types of operators in PHP are: arithmetic operators, array operators, assignment operator, comparison operators, decrement/increment operators, and logical operators.

We will take each type, one by one with a simple explanation and follow each section with simple examples. Naturally, as we proceed through the tutorials we will see this operators again and again. The ones you may have difficulty in remembering will become old friends by the end of this tutorial series.

The Arithmetic Operators

The arithmetic operators are:

  • Addition operator represented by the plus sign. The result of addition of two numbers is there sum. Example:

$a = 5;

$b = 7;

$a + $b results in the value of 5 + 7 = 12.

  • Subtraction. represented by the (-) minus sign.The result is the difference between the two numbers. Example:

$a = 4;

$b = 6;

$a -$b results in the value of 4 – 6 = -2.

  • Multiplication is the product of two numbers. represented by the (*) asterisk symbol Example:

$a =10;

$b= 6;

$a * $b results in the value of 10 X 6 or 60.

  • Division results in the quotient of a by b. represented by the (/) forward slash symbol. Example

$a = 15;

$b = 5;

$a / $b result in 15/5 or the value 3.

  • Modulus is the remainder of a division operation. Example:

$a = 23;

$b = 5;

$a % $b $b does not divide into $a evenly. The result of the division is 4 with a remainder of 3. The $a modulus $b is 3.

  • Negation. Is the opposite of the value. If $a is a positive number -$a is negative. If $a is negative -$a is positive

  • Concatenation. This results in a concatenation of two strings. The concatenation of a string and a number value results in a sting due to the languages weak typing.

Arithmetic Operators Examples

Simple examples which illustrate the use of arithmetic operators.
Simple examples which illustrate the use of arithmetic operators.

The Array Operators

The PHP array operators can be put into three groups: identity/npn-identity, equal/not equal, identity/non-identity, and union.

Identity/non-identity

$ x === $y; Identity. For this statement to be true.Array $x and Array $y must have the same key/value pairs in the same order and be of the same type.

$x !== $y; Nonidentity. This will be true if Array $x is not identical to Array $y.

Equal/not Equal

$x == $y; Equality. To evaluate to true, they must agree for every key/value pair, but need not be of the same type.

$x != $y; To evaluate to true, Array $x and Array $y must have some different values.

$x <> $y; To evaluate to true, Array $x and Array $y must have some differences

Union


The union operator joins two or more arrays into a single array. In code it is represented by the (+) sign.

$x + $y;

One not on the union operator. It does not remove duplicate key values.

Array Operator Examples

Simple array operations. We use var_dump() a handy built-in way to dump information about a variable.
Simple array operations. We use var_dump() a handy built-in way to dump information about a variable.

The Assignment Operators

The basic assignment operator is represented by the equals sign (=). What it means is the left-hand side of the operator (referred to as the l-operand) is assigned the value of operation as it would be performed between the 1-operand and the r-operand. There are shorthand operators for addition, subtraction, multiplication, division, modulus, and concatenation. Examples

$x += $y; is the same as $x = $ x + $y;

$x -= $y; is the same as $x = $ x - $y;

$x *= $y; is the same as $x = $ x * $y;

$x /= $y; is the same as $x = $ x / $y;

$x %= $y; is the same as $x = $ x % $y;

$x .= $y; is the same as $x = $ x. $y;

The Assignment Operators

Some simple assignment operators. These are really just shortcut ways of eleminating keystrokes.
Some simple assignment operators. These are really just shortcut ways of eleminating keystrokes.

The Comparison Operators

The comparison operators come in pairs: identical/non-identical, equal/not equal, great than/less than,, greater than or equal to/less than or equal to.

identical/non-identical

$x === $y is true if the values are the same and they are of the same type.

$x !== $y is true if they differ in type or values

equal/not equal

$x == $y is true if they agree in values. They do not have to agree in type.

$x != $y is true if the have different values.

greater than/less than

$x > $y is true if the value of $x is greater than the value of $y.

$x < $y is true if the value of $x is less than the value of $y.

greater than or equal to/less than or equal to

$x >= $y is true if the value of $x is greater than or equal to the value of $y.

$x <= $y is true if the value of $x is less than or equal to the value of $y.

The Comparison Operators

Simple comparison operator. You might be unfamiliar with === and !==. Also, note that !nul evealues to nullbool(true) while null evaluates to null.
Simple comparison operator. You might be unfamiliar with === and !==. Also, note that !nul evealues to nullbool(true) while null evaluates to null.

Decrement/Increment Operators

There are four operators in this class: two pre- and two- post operands. The distinguish feature is when the value of the variable is changed.

The pre-operators:

++$x; $x is first incremented by 1 and then is available for use.

--$x; $x is decremented by 1 and then is available for use.

The post-operators:

$x++; $x is evaluated and used, then incremented.

$x--; $x is evaluated and used, then decremented.


Examples of the Use of Decrement Operators

When a variable is pre-incremented ., the new value is seen in the echo statement. When it it post-incremented the new value is not reflected as of yet.
When a variable is pre-incremented ., the new value is seen in the echo statement. When it it post-incremented the new value is not reflected as of yet.

The Logical Operators

The logical operators are and, or, exclusive or, and not.

The and operator can be expressed two different ways:

$x and $y is true only if $x is true and $y is true. It can also be expressed as $x && $y.

The or operator can be expressed two different ways as well.

$x or $y is true if at least one of them is true. It can also be expressed as $x || $y.

The exclusive or is expressed as xor. $x xor $y is true if either $x or $y is true, but not both.

The not operator !$x. This is true if $x was false. (or null????)


The Logical Operators

In this example, the values for $x and $y were set to 1 (true). Note that an exclusive or (xor) returns a value of false,
In this example, the values for $x and $y were set to 1 (true). Note that an exclusive or (xor) returns a value of false,

Wrap Up and What's Next

In the tutorial we covered all of the categories of operations that are found in the PHP language. Some of the items for review that you as a developer should consider are:

  • the result of a logical expression when the variables are numbers
  • the result of a logical expression when the "null" term is used
  • and one thing we have not covered as yet, operator precedence when more than two arguments are be compared. The suggestion for right now is when in doubt use parenthesis to group the terms. For example, what would you do in the case of:

!$x || $y && $z xor $w

What would be the result given various values for the xariables $x, $y, $z, and $w

In the next tutorial we will start investigating conditional statement. Sever such as the if...else was used rater informally in some of the examples.The next tutorials will be more rigorous in out treatment of them.

Rate is Tutorial on Clarity

5 out of 5 stars from 1 rating of Was the information in this tutorial understandable?

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)