Page 1 of 1
Simple php question(s)
Posted: Sat Oct 28, 2006 10:17 am
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...
,
often used like this:
as in something like this:
Also don't know how to interpret this (or similar):
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
Posted: Sat Oct 28, 2006 10:56 am
by akimm
if(!$j) $j = 0;
yes. ! evaulates the opposite,
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.
Re: Simple php question(s)
Posted: Sat Oct 28, 2006 12:53 pm
by Chris Corbyn
michlcamp wrote:simple things like double "+" signs,
++ is an increment operator used on integers.
For example:
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...
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):
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:
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";
Posted: Sat Oct 28, 2006 1:20 pm
by michlcamp
wow..thanks a million...great help - above and beyond!
mc
Posted: Sat Oct 28, 2006 1:47 pm
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.
Posted: Sat Oct 28, 2006 4:00 pm
by volka
It does? For integers?
Source?
Posted: Sat Oct 28, 2006 4:13 pm
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.
Posted: Sat Oct 28, 2006 11:45 pm
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
-wyrmmage