Simple Machines Community Forum

Archived Boards and Threads... => Archived Boards => Parham's PHP Tutorials => Topic started by: Parham on August 30, 2003, 06:12:09 PM

Title: PHP Lesson 06 - operators
Post by: Parham on August 30, 2003, 06:12:09 PM
We've already discussed two operators in our lessons up to this point.  The first one and probably the most important operator we discussed was the assignment operator ("=").  This operator allowed us to assign values to variables and arrays (which we recently learned about).  The other operator we discussed was the array operator ("=>") which allowed us to match array "keys" to array "values".  There are also the "concatenation" operators which combine strings.  Here are examples of them all:


$variable = 'name'; //assign "name" to the variable $variable using the assignment operator
echo "$variable\n"; //remember the "newline" character I taught last lesson...



$array = array('first' => 'something','second' => 'something else'); //assign array keys and values
print $array['first'] . "\n"; //print the array value with key "first"; concatenation operator used
/* there is also a lesson to be learned here, you can't print arrays inside double quotes like you would variables.
You either have to assign the specific array element to a variable and print that or use the concatenation method
which we learned in lesson 4 */


Pay particular attention to the above comments as some of them were not discussed in previous lessons.  In this lesson I'll go over some other common operators you might want to use.

The arithmetic operators are used to do simple mathematical operations such as addition, subtraction, multiplication, and division.  Here are examples of each:


$number1 = 12;
$number2 = 3;

$answer = $number1 + $number2;
echo "$answer\n"; //prints 15
$answer = $number1 - $number2;
echo "$answer\n"; //prints 9
$answer = $number1 * $number2;
echo "$answer\n"; //prints 36
$answer = $number1 / $number2;
echo "$answer\n"; //prints 4


A few other common operators you will find yourself using often are the following:

Here is a combination operator, concatenation and assignment in one.  This operator takes the original string and adds a new string onto it.


$string = 'hello'; //assign our original string
$string .= ' there'; //concatenate a new string onto it using the ".=" operator
echo "$string\n"; //prints "hello there";


Here is a combination operator, addition and assignment in one.  This operator takes the original number and adds a new number to it.


$number = 12; //assign our original number
$number += 3; //add a new number to it using the "+=" operator
echo "$number\n"; //prints "15"


That's about it for this lesson.  There really isn't much to teach about operators, you use them as you need them.  And if you don't know if what you want exists, you just check the PHP documentation.

Check out PHP's documentation for explanations of all the operators:
http://www.php.net/manual/en/language.operators.php
Title: Re: PHP Lesson 6 - operators
Post by: Palli Moon on November 15, 2003, 01:40:41 PM
Hmm the \n command in my computer just does the same as the space button not the enter, do you know what's wrong?
Title: Re: PHP Lesson 6 - operators
Post by: [Unknown] on November 15, 2003, 03:42:47 PM
Use <br />, which is the HTML equivalent to \n.

-[Unknown]
Title: Re: PHP Lesson 6 - operators
Post by: pulpitfire on November 17, 2003, 12:44:13 AM
These lessons are just what I was looking for.  The simple and brief explanations with the examples really help me to understand.  By the way, Palli Moon, how is it that you have zero posts after posting that message?
Title: Re: PHP Lesson 6 - operators
Post by: Tyris on November 17, 2003, 04:14:27 AM
in SMF, forums can be made so that all posts in them count for zero (useful for saaay.. a test board).
For some reason this has been done for this board... I guess so Parham doesn't get like... 16 extra posts ;)?
Title: Re: PHP Lesson 6 - operators
Post by: pulpitfire on November 17, 2003, 10:54:33 AM
Quote from: Tyris on November 17, 2003, 04:14:27 AM
in SMF, forums can be made so that all posts in them count for zero (useful for saaay.. a test board).

I should have figured that.  YaBB SE even has that option. 

Quote
For some reason this has been done for this board... I guess so Parham doesn't get like... 16 extra posts ;)?

Yeah, or it could be to keep people from chatting up the topic just to get posts :)
Title: Re: PHP Lesson 6 - operators
Post by: Tyris on November 18, 2003, 02:40:42 AM
cant see the point... we do it everywhere else... and do it here too ;)
Title: Re: PHP Lesson 6 - operators
Post by: cpeppler on December 04, 2003, 03:11:31 PM
I have a question.  I have been working with PHP for a while, but I just ran across an operator (in some working code) that I have never seen before.  Here is the code snippet:
$uch =& $used[$ch]

Can anyone identify the "=&" operator?  I looked all through the operators on php.net, and found nothing.

$used[$ch] is an array, and as I look at the code later on, it looks like $uch is being used as an array as well. 

If you can send me a link to any reference docs, that would be great.  I just can't find it!

TIA

Charlie
Title: Re: PHP Lesson 6 - operators
Post by: [Unknown] on December 04, 2003, 03:54:45 PM
It's not an operator. (or it sorta is, depending....)  And I've ALWAYS hated it put like that, probably my C++ background there...

What it is is a reference.  This means, it uses the other variable to REFER to the second.  This can get complicated.... basically:

$test = 5;
// I always put the & before the $ because it makes more sense to me that way.  Seems logical.
$temp = &$test;
echo $temp; // Output: 5
echo $test; // Output: 5
$temp = 6;
echo $test; // Output: 6

It's really sometimes a bad idea to use references unless:
  - you're worried about memory, because creating a new variable can eat more ram.
  - you need a variable to change another variable automatically. (for example, so you don't have to rewrite ALL your other code.)

And remember, references ARE slower than regular variables.  The worst reason to use one is lazyness: you don't want to type something long.  But, sometimes that's neccessary - if it's so long it's confusing, using a reference might be beneficial - for example, with arrays within arrays within arrays within arrays... and, it might even be faster than crawling down the tree in that case.

-[Unknown]
Title: Re: PHP Lesson 6 - operators
Post by: Parham on December 05, 2003, 02:49:05 AM
cpeppler: http://www.simplemachines.org/community/index.php?topic=1719 for the tutorial on referencing :).  It's not full of details like Unknown's last post here, but it'll explain to how how they work with examples :)
Title: Re: PHP Lesson 06 - operators
Post by: w1x on October 06, 2005, 07:34:24 AM
Hi,
im completely new to php and like the tutorials thanks for them.  Just a stupid question:

$string = 'hello'; //assign our original string
$string .= ' there'; //concatenate a new string onto it using the ".=" operator
echo "$string\n"; //prints "hello there";

why add there in a seperate string?  why not just:

$string = 'hello there';

???
Title: Re: PHP Lesson 06 - operators
Post by: Oldiesmann on October 06, 2005, 05:39:04 PM
Because we can :P

He was just demonstrating how you can use ".=" to append something to an existing string (useful when you want to change part of a string depending on the situation, such as whether they're a user or a guest).
Title: Re: PHP Lesson 06 - operators
Post by: w1x on October 07, 2005, 12:52:17 PM
oo get it now  ;) thanks
Title: Re: PHP Lesson 06 - operators
Post by: Parham on October 07, 2005, 02:57:58 PM
For example (very bad example)... what if we wanted to append 200 ' there' words behind 'hello'?  You wouldn't want to do that manually.


<?
$string = 'hello'; //assign our original string
foreach (range(0,200) as $i) {
$string .= ' there'; //concatenate a new string onto it using the ".=" operator
}
echo "$string\n"; //prints "hello there there there there ...";
?>