Cannot redecalre forum<

liunx

Guest
I don't need help with my original post, so I just figured I'd conserve posts and just edit this one.

I got the link to the lastest posts hack for my site. I wanted it to display the latest posts, regardless if they're considered new or not.

I was looking at the code they wrote and there is one part (well, there's alot of parts, but this is the first part) that I don't understand.

Here is the section of code:


// No forum need be specified

$n = isset($this->get['n']) ? intval($this->get['n']) : $this->sets['topics_per_page'];
$min = isset($this->get['min']) ? intval($this->get['min']) : 0;

$this->set_title("Active topics since last visit");

$forums_str = "(";
$query = $this->db->query("SELECT forum_id FROM {$this->pre}forums");
while ($row = $this->db->nqfetch($query))
{
if ($this->perms->auth('forum_view',$row['forum_id']))
{
if (strcmp($forums_str, "("))
{ $forums_str .= ","; }
$forums_str .= $row['forum_id'];
}
}
$forums_str .= ")";



The line I don't understand is
$min = isset($this->get['min']) ? intval($this->get['min']) : 0;

The part I dont' get is how min is all over the place. Does it mean that $min = somethign if min is set?

I'm uber-confused :(

Sorry I'm so slow on thisand why is the 0 on the end?that is kindof a shorthand way of writing an if statement
//this code:
$min = isset($this->get['min']) ? intval($this->get['min']) : 0;

//is the same as saying this:
if( isset($this->get['min']) )
$min = intval($this->get['min']);
else
$min = 0;


All it is saying is that if min exists in the class that $this is pointing to, then set it to $min, otherwise set $min to zero.ah okie dokey, thank you very very much :DAh ternary conditions. Don't cha jus' luv em! :)

I love using ternaries as they save several lines of code, the downside is that they do make your code less readable. Good to learn though.ternary conditions? That's what the thingy they did above is called?Yup, not just specific to PHP either. You see them in C, Java, and many other languages. Nice and efficient, just not too readable (until you get used to them).

Exactly as IKLOP says, if you ever find yourself writing lots of:if ([condition]) {
$foo = $value1;
} else {
$foo = $value2;
}
and you want to reduce the number of lines of code, then you can consider rewriting them as:
$foo = ([condition]) ? $value1 : $value2;Originally posted by torrent
Yup, not just specific to PHP either. You see them in C, Java, and many other languages. Nice and efficient, just not too readable (until you get used to them).

Exactly as IKLOP says, if you ever find yourself writing lots of:if ([condition]) {
$foo = $value1;
} else {
$foo = $value2;
}
and you want to reduce the number of lines of code, then you can consider rewriting them as:
$foo = ([condition]) ? $value1 : $value2;

Ok, so in this code:

$min = isset($this->get['min']) ? intval($this->get['min']) : 0

Isn't that saying that if min is set, set min to min else 0? That doesnt' make sense to meNot quite. What it's saying is "If the object variable called 'min' is not null (i.e. it has a value) then set the variable $min (not the object one) to the integer of portion of its value, otherwise set it to 0"

So, if $this->min is equal to 20.3 then this line of code will set $min to equal 20. If $this->min is not set (null) then it will set $min to equal 0but it has the $this-> everytime it has $min, doesn't that mean it's using the object one?Okay, just looking at your snippet more closely. It's setting $this->min to the integer portion of $this->get['min'] (a different variable), or zero if $this->get['min'] is not set.

Sorry, I never really looked too deeply at the actual snippet you posted in your first post as the question started out just being about ternaries :)I get the original point, I'm still kinda confused, but that's ok :) ...everything php wasn't learned in a day.now correct me if I am wrong but $min is a different variable then $this->min is it not?

$min = isset($this->get['min']) ? intval($this->get['min']) : 0;

$this->min = isset($this->get['min']) ? intval($this->get['min']) : 0;

those set two different variables right?

seeing how they didn't use $this-> in the function means it can't be used in the class, right? I think that is where Vy is getting confused? unless I am :PI'm super confused..I try to explain what I dont' get again cause I'm not explaining it well.

Ok, we have this

$min = isset($this->get['min']) ? intval($this->get['min']) : 0;


The part that I'm confused on is this part:

($this->get['min'])

It's in there twice. Doesn't that mean they're setting it to the same thing or no?just like IKLOP said


//is the same as saying this:
if( isset($this->get['min']) )
$min = intval($this->get['min']);
else
$min = 0;

the second part is this

$min = intval($this->get['min']);
in the above scenerio

so read it like this

if isset($this->get['min']) is true ? assign this value intval($this->get['min']) to $min else :assign this to $min 0;

does that make more sense?I'm sorry scoutt, but no. But, when you put it like that, it's easier to explain what I'm thinking :D

Ok, You said:


if isset($this->get['min']) is true ? assign this value intval($this->get['min']) to $min else :assign this to $min 0;

Ok, so isn't it saying that if get['min'] is set, then make it equal to $this->get['min']) otherwise set it = to 0?

I don't see why one would do that..so I figured I was misunderstanding what they were doing.

Sorry to be a problem person today scouttOriginally posted by Low_Overhead
IOk, so isn't it saying that if get['min'] is set, then make it equal to $this->get['min']) otherwise set it = to 0?Nope, it's saying:

if get['min'] is set, then make $min, not $this->get['min'], equal to the whole number portion of $this->get['min']) otherwise set it = to 0?

Scenario:
$min = 0
$this->get['min'] = 20.3

The ternary would result in $min equalling 20 (not 20.3 because of the intval() function being applied)

Scenario
$min = 0
$this->get['min'] = '' (i.e. null)

The ternary would result in $min equalling 0think of it this way. if we were to compare the two it would be like this


//is the same as saying this:
if( isset($this->get['min']) )
$min = intval($this->get['min']);
else
$min = 0;

$min = isset($this->get['min']) ? intval($this->get['min']) : 0;


this part isset($this->get['min']) is the same as if( isset($this->get['min']) ) and this ? is the same as { and intval($this->get['min']) is the same as $min = intval($this->get['min']); and : 0; is the same as else $min = 0;:D Ok, I think I got it now :D ...thanks guys, sorry for being so...dense about it :)
 
Back
Top