p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Help with an if statment!


Message #1 by "Chris Kenna" <webmaster@c...> on Sat, 25 May 2002 21:11:17
I know that this is a simple question, but I am having much difficulty 
with this if statment.  I wrote a script that gets a variable from a URL 
(path to a file), and then prints the file in the area where it is told 
to do so.  The if statment below is ment to check to see if the filename 
variable is proceeded by a slash, and if so leave it alone.  If not, it 
is supposed to add one.  The statment has no problem adding a slash, but 
if a slash already exists, then it adds one anyway.  I have tried many 
different ways of doing this, but none seem to work.  Any one know a 
better way?

THE IF STATMENT:

$slashpos = strpos($file, "/"); // find slash position
if ( $slashpos != 0 || $slashpos == NULL ){ // if slash is not 0, or if 
slash is non existant, add a slash
	$file = "/" . $file;
}

Thanks for the help,
Chris
Message #2 by "Tonsil" <tonsil@s...> on Mon, 27 May 2002 01:22:03 -0400
Kick it like this:
if ( ord($file[0]) != 47 ) { // if 1st char in $file is not '/', add a slash
  $file = "/" . $file;
}

On 25 May 02, at 21:11, Chris Kenna wrote:

> I know that this is a simple question, but I am having much difficulty 
> with this if statment.  I wrote a script that gets a variable from a URL 
> (path to a file), and then prints the file in the area where it is told 
> to do so.  The if statment below is ment to check to see if the filename 
> variable is proceeded by a slash, and if so leave it alone.  If not, it 
> is supposed to add one.  The statment has no problem adding a slash, but 
> if a slash already exists, then it adds one anyway.  I have tried many 
> different ways of doing this, but none seem to work.  Any one know a 
> better way?
> 
> THE IF STATMENT:
> 
> $slashpos = strpos($file, "/"); // find slash position
> if ( $slashpos != 0 || $slashpos == NULL ){ // if slash is not 0, or if 
> slash is non existant, add a slash
> 	$file = "/" . $file;
> }
> 
> Thanks for the help,
> Chris
> 



_________________________________________________
This email has been brought to you by:
     House Arrest Daycare (tm)
"Hey, I figgered I wasn't goin' nowhere nohow..."
Message #3 by "Nikolai Devereaux" <yomama@u...> on Tue, 28 May 2002 10:56:27 -0700
> I know that this is a simple question, but I am having much difficulty
> with this if statment.  I wrote a script that gets a variable from a URL
> (path to a file), and then prints the file in the area where it is told
> to do so.  The if statment below is ment to check to see if the filename
> variable is proceeded by a slash, and if so leave it alone.  If not, it
> is supposed to add one.  The statment has no problem adding a slash, but
> if a slash already exists, then it adds one anyway.  I have tried many
> different ways of doing this, but none seem to work.  Any one know a
> better way?
>
> THE IF STATMENT:
>
> $slashpos = strpos($file, "/"); // find slash position
> if ( $slashpos != 0 || $slashpos == NULL ){ // if slash is not 0, or if
> slash is non existant, add a slash
> 	$file = "/" . $file;
> }

It seems that your conditional expression always returns true.

It's probably because when PHP evaluates a boolean expression, NULL, false,
0, and the string "false" are all treated as false.  Because of this, you
always end up with an if statement of this form:

((x != false) || (x == false))

Simplifying this expression using simple boolean algebra gives us:

((x) || (~x))
(true)



There are three possible values of $slashpos:  false, 0, and non-zero.


False:
The first half of your expression tests $slashpos != 0.  If there is no
slash in the url, then $slashpos is false, which is (thanks to dynamic type
coersion) and is actually equivalent to zero.  Therefore we go to the second
half of your expression.

PHP here compares false and NULL in a boolean expression, and those two are
equivalent -- both are treated as false in that context.


Zero:
The first half obviously fails because (0 != 0) returns false.  So we look
at the second part.  (0 == NULL) returns true because neither NULL nor 0 are
boolean values, so they are both treated as 'false' within the scope of this
comparison.  So false == false returns true.


NonZero:
The first half returns true because nonzero integers are treated as true
values.  true != false returns true.


Recommendations:
If you're going to test integer values in an if statement, you're better off
using the new equivalence operators (=== and !==) to test TYPE equivalence.

1 == true  // returns true
1 === true // returns false

That way you don't end up with type coersion fouling up your semantic
intent.

Also -- it's useless to test for a variable being null right after you
assign a value to that variable.

I'd rewrite your conditional to this:

if('/' == $file[0])

Simple, easy to read, to the point.  no?


take care,

nik

Message #4 by "Nikolai Devereaux" <yomama@u...> on Tue, 28 May 2002 11:05:40 -0700
> Kick it like this:
> if ( ord($file[0]) != 47 ) { // if 1st char in $file is not '/',

That will work, but using the ord() function is unnecessary overkill since
PHP can handle == operations on strings.

nik

Message #5 by "Tonsil" <tonsil@s...> on Tue, 28 May 2002 15:27:49 -0400
Yeah, and the phrase "unnecessary overkill" is redundant!  :p

I think it's time programmers the world over knocked off early and 
grabed a few beers... who's with me? Hehe

On 28 May 02, at 11:05, Nikolai Devereaux wrote:

> 
> > Kick it like this:
> > if ( ord($file[0]) != 47 ) { // if 1st char in $file is not '/',
> 
> That will work, but using the ord() function is unnecessary overkill since
> PHP can handle == operations on strings.
> 
> nik
> 
> 
> 



_________________________________________________
This email has been brought to you by:
     House Arrest Daycare (tm)
"Hey, I figgered I wasn't goin' nowhere nohow..."
Message #6 by "Nikolai Devereaux" <yomama@u...> on Tue, 28 May 2002 13:07:16 -0700
> Yeah, and the phrase "unnecessary overkill" is redundant!  :p

Well, there's useful overkill too, so technically speaking it's not
redundant. =)

e.g.

if(isset($file) && (strlen($file) > 1) && ($file[0] == '/'))

would be overkill, but it's not unneccesary since you'd need the first two
validity checks to suppress warnings and errors if you've got a more strict
level of error reporting.

> I think it's time programmers the world over knocked off early and
> grabed a few beers... who's with me? Hehe

One step ahead of you!

And somehow, three to the left...

Message #7 by "Laurie Tsakiris" <laurietsakiris@h...> on Tue, 28 May 2002 13:20:54 -0700
you buyin? :-P

----- Original Message -----
From: "Tonsil" <tonsil@s...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, May 28, 2002 12:27 PM
Subject: [beginning_php] Re: Help with an if statment!


> Yeah, and the phrase "unnecessary overkill" is redundant!  :p
>
> I think it's time programmers the world over knocked off early and
> grabed a few beers... who's with me? Hehe
>
> On 28 May 02, at 11:05, Nikolai Devereaux wrote:
>
> >
> > > Kick it like this:
> > > if ( ord($file[0]) != 47 ) { // if 1st char in $file is not '/',
> >
> > That will work, but using the ord() function is unnecessary overkill
since
> > PHP can handle == operations on strings.
> >
> > nik
> >
> >
> >
>
>
>
> _________________________________________________
> This email has been brought to you by:
>      House Arrest Daycare (tm)
> "Hey, I figgered I wasn't goin' nowhere nohow..."
>
>
Message #8 by "Chris Kenna" <webmaster@c...> on Tue, 28 May 2002 23:20:18
Hi all.  Thanks for the help.  I managed to get it to work with: 
( strpos($file, "/") != 0 || strpos($file, "/") === false )

Thanks to all who helped, and I know that there is a better way.  I am 
still learning PHP, does anyone have any place that they know is a good 
place to start learning, a book perhaps?
Message #9 by "Nikolai Devereaux" <yomama@u...> on Tue, 28 May 2002 15:27:48 -0700
> Hi all.  Thanks for the help.  I managed to get it to work with:
> ( strpos($file, "/") != 0 || strpos($file, "/") === false )

Hi again,

The problem with this is that you're making two calls to strpos.  Strpos is
a useful function for finding the position of the first instance of a
character in a string.  Since you already know WHERE you expect the slash to
be, it makes more sense to test that specifically with $file[0].  Your code
is simpler, much more readable and maintainable, and executes quicker.


> Thanks to all who helped, and I know that there is a better way.  I am
> still learning PHP, does anyone have any place that they know is a good
> place to start learning, a book perhaps?

Are you serious?  This _IS_, as you hopefully know, a support forum for the
Wrox "Beginning PHP" books...

Most of the stuff I've learned about PHP hasn't really been from books, to
be honest.  I bought a couple starter books (at the time, the only two
available were Wrox Professional PHP Programming and CORE PHP3).  They both
served a purpose in that I was given a brief familiarity with the language,
but I learned more from reading the PHP.net manual and articles at
phpbuilder.com.


good luck,

nik


  Return to Index