• Welcome to Simple Machines Community Forum. Please login or sign up.
January 21, 2022, 01:02:34 AM

News:

Want to get involved in developing SMF, then why not lend a hand on our github!


PHP Lesson 06 - operators

Started by Parham, August 30, 2003, 06:12:09 PM

Previous topic - Next topic

Parham

August 30, 2003, 06:12:09 PM Last Edit: March 29, 2004, 05:04:30 AM by [Unknown]
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

Palli Moon

Hmm the \n command in my computer just does the same as the space button not the enter, do you know what's wrong?
Palli "Bootlegger" Moon

[Unknown]

Use <br />, which is the HTML equivalent to \n.

-[Unknown]

pulpitfire

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?

Tyris

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 ;)?

pulpitfire

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 :)

Tyris

cant see the point... we do it everywhere else... and do it here too ;)

cpeppler

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 hxxp:php.net [nonactive], 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

[Unknown]

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]

Parham

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 :)

w1x

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';

???

Oldiesmann

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).
Michael Eshom
Cincy Space - now open!

w1x


Parham

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 ...";
?>

Advertisement: