can i shorten some php like this?

Soldato
Joined
19 Oct 2002
Posts
3,480
quick question: can i use something similar to this shortened code:

Code:
if (strpos($fname, "p.jpg"|"t.jpg") > 0){

or does it have to be more like:

Code:
if ((strpos($fname, "p.jpg") > 0) | (strpos($fname, "t.jpg") > 0){
?

funny thing is the first one seems to work but only acknowledges the filenames ending in t not p :confused:
 
cant you just add the strings? Also you cant compare to 0 as fname could occur at position 0. use === false to determine the outcome.
Code:
if (strpos($fname, "p.jpg"."t.jpg") > 0){
 
quick question: can i use something similar to this shortened code:

Code:
if (strpos($fname, "p.jpg"|"t.jpg") > 0){

No; what you're actually doing there is taking the bitwise OR of those two strings, converting it back to a string, and then using the result. This won't work as expected :p

Try using a regular expression instead:

Code:
if (preg_match('/p.jpg|t.jpg/i', $fname))
{
	// Stuff…
}

Note that this will me marginally slower than using strpos twice.

cant you just add the strings? Also you cant compare to 0 as fname could occur at position 0. use === false to determine the outcome.
Code:
if (strpos($fname, "p.jpg"."t.jpg") > 0){

That won't work as it'll test if they're both present in the string in succession.
 
Last edited:
Sorry your right, I got the parameter order wrong, thought we were searching in p.jpg t.jpg not for it. :)

That's PHP for you: horribly inconsistent function naming and parameter ordering throughout the standard library!

Edit: it's probably also worth mentioning that you shouldn't use a single pipe (|) or ampersand (&) for logical operations; instead use double pipes and ampersands: || and &&. The reason for this is that || and && are what are called short circuit operators – that is, they only evaluate their operands as necessary.

For example: when evaluating ($foo || $bar), then if $foo is true, $bar won't be evaluated (since whatever it is the outcome will still be true). The converse is true for &&.

This can be useful in a scenario where the second operand can only be evaluated if the first is true, for example:

Code:
if ($foo != null && $foo->bar())
{
	// Stuff…
}

If you were to use & in this case, then $foo->bar() would be evaluated even if $foo is null, which would cause a fatal error.

By contrast, | and & are bitwise operators, which are actually meant for manipulating individual bits in binary values. See:
http://uk2.php.net/language.operators.bitwise
 
Last edited:
all very good to know :)

the regex method above worked great :) could you tell me the purpose of the slashes either side of the expression?

/p.jpg|t.jpg/i
 
They're just delimiters that mark the beginning and end of the expression itself; anything outside the delimiters is interpreted as a modifier. The i modifier there makes it a case-insensitive match. You don't have to use forward slashes; you can use whatever symbol you like (as long as you escape it when it occurs in the expression).

I'd recommend reading up on regexes; they're awesomely powerful, if a little complex at first glance:
http://uk.php.net/manual/en/regexp.reference.php
 
Last edited:
That's PHP for you: horribly inconsistent function naming and parameter ordering throughout the standard library!
This attitude to PHP makes me laugh. Wasn't it PHP that you used to learn pretty much everything you know? Or at least put into practice?

It may also just be me, but I've only noticed .NET'ers "whinge" about PHP.. no one else does.
 
Wasn't it PHP that you used to learn pretty much everything you know? Or at least put into practice?

Not really, but then at no point did I say it's not a useful or effective language, either. Ineed I very much prefer PHP to ASP.NET for web development.

It may also just be me, but I've only noticed .NET'ers "whinge" about PHP.. no one else does.

I think it's just you.
 
Last edited:
Nope I actually agree with him :)

OK well I have to say it's not something I've ever noticed. In my experience, most of the PHP hate comes from people using languages similar in market/purpose like Ruby and Python. .NET and PHP serve completely different purposes.

I have to say I do miss a lot of the features from .NET when using PHP, though.
 
PHP is my bread and butter but the inconsistencies are definitely an annoyance...

strpos vs str_replace - why do some string function names have underscores and others do not?

strpos($haystack, $needle)
vs
in_array($needle, $haystack)


It's not something that will stop me using PHP, it's just one of the quirks of the language, but it would have been nice if for v6 they thought "sod backwards compatibility" and tidied it up.
 
Back
Top Bottom