PHP Operators

What are operators? Things you can execute operations with. PHP has different kinds of operators:

Arithmetic operators

For Mathematical operations. Choose of the following:

  • + Addition
  • – Subtraction: Subtracts a value from a variable
  • * Multiplies
  • / Divides
  • % Modulus: Gives the remaining number when using division
  • ++ Increments
  • – – Decrements

Note: The increment/decrement operators only affect numbers and strings. Arrays, objects and resources are not affected. Decrementing NULL values has no effect too, but incrementing them results in 1.

– by php.net

Assignment Operators

Assigning Values to Variables.

  • = Variable equals number
  • += Adds value to variables (not replaces variable)
  • -= Decrease value from variable
  • *= Multiplies value to variable
  • /= Divides value from variable
  • .= used in strings to concatenate: $j.=$k is equivalent to $j . $k
  • %= Modulus

Comparison Operators

Often used in if statements when comparing 2 items

  • == is equal to // NOTE: Don’t mix up with assignment operator =
  • != is not equal to
  • > greater than
  • < less than
  • >= greater than or equal to
  • <= less than or equal to

Logical Operators

Usually it is used to combine the results of other operators and returns true or false.

  • && and
  • and Low-precedence and
  • || or
  • or Low-precedence or
  • ! not
  • xor Exclusive or
Advertisements

How to Open a PHP File from your Localhost

And another one from these life saving tips and problem-solving where the answer is so simple that people don’t even understand the question and you feel like the dumbest thing ever afterwards.

Problem: You set up your local development environment, you have apache running and set up to read php files, you installed an integrated development environment (IDE), and to make things easier for you your partner even helped you to find the files in your Finder so you don’t have to use bash to often (which most of the times still gives me this cryptic white characters on black screen feeling).

Then you try to open a php file which you know worked before, but suddenly your browser just won’t interpret it anymore. No matter how often you click on it in Finder, it just gives you the option to open it with a text editor.

So what went wrong?

Apparently, your browser can’t read php files directly, it needs to run it through an interpreter who does the work for it and returns just a normal html file which the browser can handle fine (that’s why html files can easily be opened without Apache or another interpreter directly from the computer). “But I did all this!” you may cry out. Which is true, but if you just try to open it from the computer directly your browser isn’t smart enough to ask apache for interpretation. It just treats it as a normal file. For the file to be shown in the browser you actually need to type in the URL in the browser window, and then the browser will go and look for the file on your computer (or wherever it is hosted) and the whole process of asking apache to interpret the file and then having it returned as a readable html can start.

Boom. It’s that easy, if you know it. So just make sure to know the URL of your file and you are good to go. Whoohoo.