Advertisement:

Author Topic: PHP Lesson 06 - operators  (Read 23220 times)

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
PHP Lesson 06 - operators
« 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:

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

Code: [Select]
$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:

Code: [Select]
$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.

Code: [Select]
$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.

Code: [Select]
$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
« Last Edit: March 29, 2004, 05:04:30 AM by [Unknown] »

Offline Palli Moon

  • Newbie
  • *
  • Posts: 2
  • Gender: Male
  • I'm a llama!
Re: PHP Lesson 6 - operators
« Reply #1 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?
Palli "Bootlegger" Moon

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP Lesson 6 - operators
« Reply #2 on: November 15, 2003, 03:42:47 PM »
Use <br />, which is the HTML equivalent to \n.

-[Unknown]

Offline pulpitfire

  • Sophist Member
  • *****
  • Posts: 1,132
  • Gender: Male
  • This is what the php parser looks like.
    • Pulpitfire Devotionals
Re: PHP Lesson 6 - operators
« Reply #3 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?

Offline Tyris

  • Sophist Member
  • *****
  • Posts: 1,306
  • Gender: Male
    • Shana Project
Re: PHP Lesson 6 - operators
« Reply #4 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 ;)?

Offline pulpitfire

  • Sophist Member
  • *****
  • Posts: 1,132
  • Gender: Male
  • This is what the php parser looks like.
    • Pulpitfire Devotionals
Re: PHP Lesson 6 - operators
« Reply #5 on: November 17, 2003, 10:54:33 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 :)

Offline Tyris

  • Sophist Member
  • *****
  • Posts: 1,306
  • Gender: Male
    • Shana Project
Re: PHP Lesson 6 - operators
« Reply #6 on: November 18, 2003, 02:40:42 AM »
cant see the point... we do it everywhere else... and do it here too ;)

Offline cpeppler

  • Newbie
  • *
  • Posts: 1
  • Gender: Male
  • I'm a llama!
Re: PHP Lesson 6 - operators
« Reply #7 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:
Code: [Select]
$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

Offline [Unknown]

  • SMF Friend
  • SMF Master
  • *
  • Posts: 36,102
  • Gender: Male
Re: PHP Lesson 6 - operators
« Reply #8 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]

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re: PHP Lesson 6 - operators
« Reply #9 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 :)

Offline w1x

  • Semi-Newbie
  • *
  • Posts: 12
Re: PHP Lesson 06 - operators
« Reply #10 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';

???

Offline Oldiesmann

  • SMF Friend
  • SMF Super Hero
  • *
  • Posts: 24,892
  • Gender: Male
  • Ask me about the function DB :)
    • oldiesmann on Facebook
    • Oldiesmann on GitHub
    • https://www.linkedin.com/in/michaeleshom on LinkedIn
    • @oldiesmann on Twitter
    • Archie Comics Fan Forum
Re: PHP Lesson 06 - operators
« Reply #11 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).
Michael Eshom
Cincy Space - now open!

Offline w1x

  • Semi-Newbie
  • *
  • Posts: 12
Re: PHP Lesson 06 - operators
« Reply #12 on: October 07, 2005, 12:52:17 PM »
oo get it now  ;) thanks

Offline Parham

  • SMF Friend
  • Sophist Member
  • *
  • Posts: 1,317
  • Gender: Male
  • #!/usr/bin/perl
    • Online Node
Re: PHP Lesson 06 - operators
« Reply #13 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.

Code: [Select]
<?
$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 ...";
?>