Simple php question(s)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Simple php question(s)

Post by michlcamp »

I'm familiar with basic php and use a lot of it with mySQL and web site templates, and I've been writing some basic and functional scripts for about a year and can usually understand other scripts as I come across them, but 'm trying to advance a little futher into PHP and clearly don't know what some of the most common syntax means...simple things like double "+" signs,

and specifically this one...

Code: Select all

->
,

often used like this:

Code: Select all

$this->
as in something like this:

Code: Select all

$this->tagtracker["$curtag"]++;

Also don't know how to interpret this (or similar):

Code: Select all

if(!$j) $j = 0;
Does it mean "If not $j, then $j = zero?


obviously it's very basic stuff that I need to undestand as I progress, but I've been looking around for a good tutuorial and haven't found it yet. Hoping you might help...any quick short version answers much appreciated.

Thanks in advance.
mc
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Post by akimm »

if(!$j) $j = 0;
yes. ! evaulates the opposite,

Code: Select all

if(!1)  {
if not 1

$this->tagtracker["$curtag"]++;
This is with OOP(object oriented programming, are you sure you want to dive into that first? Why not perfect procedural coding first, thats what I'm *attempting* to do.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Simple php question(s)

Post by Chris Corbyn »

michlcamp wrote:simple things like double "+" signs,
++ is an increment operator used on integers.

For example:

Code: Select all

$i = 0;

$i++;

echo $i ; // 1
It can be done post-increment ($i++) or pre-increment (++$i). For the most part it doesn't affect anything but if you have the ++ as part of another statement the difference comes into play.

$i++ is post-increment and it means that the value of $i is read BEFORE it is incremented. So in the excerpt:

Code: Select all

$i = 5;

if ($i++ == 5) echo "i is 5"; //This prints since $i is read BEFORE incrementing

//$i is now 6
++$i is pre-increment and means that $i is incremented and THEN read.

So the same code above no longer prints "i is 5" since it is 6 before the comparison is made:

Code: Select all

$i = 5;

if (++$i == 5) echo "i is 5"; //This never prints since $i is 6

//$i is now 6
and specifically this one...

Code: Select all

->
It's an object operator. Objects are instances of classes. Classes are containers for a set of values on some actions to perform upon them. So in the simple class:

Code: Select all

class Foo
{
    var $name = "My name";
    
    function getName()
    {
        return $this->name;
    }
}

$obj = new Foo();
echo $obj->getName();
$obj is an instance of "Foo". Foo contains a variable (technically its a "property") $name. To access $name inside Foo you would use the "->" operator. To access the function (technically it's a "method") "getName()" inside Foo you also use this operator.

The reason you see $this is becuase it's a special case. From inside any class you can refer to the instance of the object that's been created from it by using $this just like we used $obj outside of the class.
Also don't know how to interpret this (or similar):

Code: Select all

if(!$j) $j = 0;
Does it mean "If not $j, then $j = zero?
You're right. ! is a negation operator. It turns a boolean (true or false) value into it's opposite. In the case where you say:

Code: Select all

if (!i) echo "Not 1";
It's basically "if not 1" but to be picky it really means "if the value of 1 is NOT true". So this does what?

Code: Select all

if (!(!1)) echo "Condition valid";
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

wow..thanks a million...great help - above and beyond!
mc
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

also, something that not many people know:

i++ creates a temporary variable and incriments it by one, then assigns the temporary variable to i, whereas ++i directly increments i by one.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

It does? For integers?
Source?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

void zend_do_pre_incdec(znode *result, znode *op1, int op TSRMLS_DC)
{
	zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

	opline->opcode = op;
	opline->result.op_type = IS_VAR;
	opline->result.u.EA.type = 0;
	opline->result.u.var = get_temporary_variable(CG(active_op_array));
	opline->op1 = *op1;
	*result = opline->result;
	SET_UNUSED(opline->op2);
}


void zend_do_post_incdec(znode *result, znode *op1, int op TSRMLS_DC)
{
	zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);

	opline->opcode = op;
	opline->result.op_type = IS_TMP_VAR;
	opline->result.u.var = get_temporary_variable(CG(active_op_array));
	opline->op1 = *op1;
	*result = opline->result;
	SET_UNUSED(opline->op2);
}
Pretty similar looking. Post's use of IS_TMP_VAR would suggest it is creating a temporary variable. Not that I consider using one over the other all that detrimental to performance; there's far larger fish to go after first.
wyrmmage
Forum Commoner
Posts: 56
Joined: Sat Oct 28, 2006 12:43 pm
Location: boise, ID

Post by wyrmmage »

ya, that's true, it's not like it's really a big deal to have just one temporary variable, just thought I'd point it out as trivia :P

-wyrmmage
Post Reply