Little known PHP Idioms etc.

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Little known PHP Idioms etc.

Post by Ollie Saunders »

...or at least little known to me :P
<rant>
  • Last night I had this family get together thing with some distant cousins of mine. I had to stay the night over in their house. The whole experience was pretty uninteresting and, expecting as such, I took mi Mac lappy with me to do some work.

    Being a freelance programmer, I don't go to bed until about 4 am usually and I wasn't about to make an exception just because I was in somebody else's house so I started using my laptop after everyone went to bed and I suddenly realised that for the first time in quite some time I was without access to the Internet. I stay at friends houses quite routinely but they all have Internet and I'm usually too engrossed in my work (and posting on DevNet ;)) to actually think about going away properly for any length of time; this is a bane of home working that I will be glad to see the back of.

    So being there, in a room all on my own I suddenly realised how much I wanted the Internet. I missed DevNet, MSN, blogs, the PHP manual, randomly being able to search stuff.

    I felt a bit like Neo in that scene in the Matrix (the first one not any of that sequel poop) where Neo gets a visitor looking for some kind of mechanise that he has "unplug" some time right before he sees the white rabbit. I shouldn't be freelancing too much longer so this is all going to improve.
</rant>

So anyway to ease mi withdrawals I started reading a PDF I had saved - "PHP Power Programming" and found some new stuff I didn't know about PHP. I really love it when that happens:
  • isset() - can take multiple parameters but only returns true if they are all set
  • global keyword is deprecated in favour of $GLOBALS
  • PHP does actually have the C shift left and right operators; I had just assumed it didn't because they are pretty useless
  • break accepts an optional argument of the amount of nesting levels to break out of. Does anybody do this?
  • continue also accepts an optional argument only this is the number of iterations to continue for
Also does anyone know the state of {} for string offset? I thought it was deprecated or is it just that [] for strings has stopped being deprecated.
PHP Power Programming wrote:In PHP 4, you could use [] (square brackets) to access string offsets. This support still exists in PHP 5, and you are likely to bump into it often. However, you should really use the {} notation because it differentiates string offsets from array offsets and thus, makes your code more readable.
Also what do people think about "statement list style" control structure when writing views or templates

Code: Select all

foreach ($array as $v):
?>
<b>
   <!-- lots of HTML goes here -->
   <?php echo $v?>
</b>
<?php endforeach;
Also I never ever managed to actually find a use for do..while but looky here
PHP Power Programming wrote:do...while loops are often used as an elegant solution for easily breaking
out of a code block if a certain condition is met. Consider the following example:

Code: Select all

do { 
    statement list 
    if ($error) { 
        break; 
    } 
    statement list 
} while (false);
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

PHP does actually have the C shift left and right operators; I had just assumed it didn't because they are pretty useless
I'm not sure I'd say useless...depends I guess on your situation(s)...
break accepts an optional argument of the amount of nesting levels to break out of. Does anybody do this?
Interesting...not sure I like the idea...but interesting none the less :)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

interesting stuff these but the only one I can really comment on is this one about 'statement list style' stuff for templates. phptal templating engine uses this stuff for writing its tmp files that parse your template. I think that is kinda the only real time to use them, when writing dynamic temp php files to parse.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Funny, sounds very familiar. Suddenly having no internet access does make you feel a bit ... weird. Like when you forgot your wallet or something. Even if you know "ok, I don't really need it this afternoon", it still feels sort of "naked" :)

About the php5 stuff: interesting stuff. I swiftly read/browsed the book as well, but didn't pick up the details.

I do like the statement list style control structure you show. It looks very clear to me. I remember how it was when I started making my first website and I didn't have any clue about this thing called PHP. I knew it was that stuff between <? ?> (for that matter, even HTML/CSS was very difficult). Even then working with the Wordpress template system, which uses this style, I could easily understand what was going on.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

In PHP 4, you could use [] (square brackets) to access string offsets. This support still exists in PHP 5, and you are likely to bump into it often. However, you should really use the {} notation because it differentiates string offsets from array offsets and thus, makes your code more readable.
I believe this is a misquote at the moment given PHP6 (AFAIK) is returning to the [] method of representing string character positions. I could be wrong here - It's been a few weeks since I last read the php mailing lists and blogs but take a gander at any PHP6 compatibility texts available to date.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

I always thought $GLOBALS was deprecated for the keyword "global;" i.e. you should use "global $var" instead of "$GLOBALS['var']"
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

  • continue also accepts an optional argument only this is the number of iterations to continue for
Doesn't seem to be true:
Php Manual wrote: continue accepts an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I'm not sure I'd say useless...depends I guess on your situation(s)...
You can just multiply or divide by two instead. Generally that it is going to be more obvious to programmers what you are doing with multiplication and division. The only use for shift left and right is performance. In compiled languages the compiler notices where it can SL and SR so you don't need specific operators and PHP is so high level it wouldn't make hardly any difference anyway. Much rather have the operators free for something else like...array appending or merging; union is good but it needs a friend.
I believe this is a misquote at the moment given PHP6 (AFAIK) is returning to the [] method of representing string character positions.
It is certainly not a misquote, I copied and pasted it. Inaccurate and out of date it may be which is why I asked for feedback. Seems Zend has done a really bad job of making developers aware of the changes that are going to be happening in PHP 6 or maybe the internals devs themselves don't know yet.
I always thought $GLOBALS was deprecated for the keyword "global;" i.e. you should use "global $var" instead of "$GLOBALS['var']"
Now that you mention it I think I thought that too. Manual page on scope makes no mention of deprecation whatsoever. Does anybody know?
Weridan wrote:Doesn't seem to be true:
OK let's test this:

Code: Select all

$a = array('foo', 'bar', 'zim', 'gir');
$b = array('dough', 'ray', 'me', 'far', 'so');
foreach ($a as $av) {
    echo $av, ' { ';
    foreach ($b as $bv) {
        if ($bv == 'me') {
            continue; // 2;
        }
        echo $bv, ' ';
    }
    echo '}<br />';
}
PHP with continue wrote:foo { dough ray far so }
bar { dough ray far so }
zim { dough ray far so }
gir { dough ray far so }
PHP with continue 2 wrote:foo { dough ray bar { dough ray zim { dough ray gir { dough ray
Appears you are right :)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

isset() - can take multiple parameters but only returns true if they are all set
8O awesome!

This is yet another example of why I love this form! You learn something new every day!
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Yeah my mouth was open when I read that one too.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The shift operators are important when doing bit level manipulations. That's overall it. Not many will ever get into that stuff and a lot can be precalculated, not that everyone does such a thing. :)
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

It is certainly not a misquote, I copied and pasted it. Inaccurate and out of date it may be which is why I asked for feedback. Seems Zend has done a really bad job of making developers aware of the changes that are going to be happening in PHP 6 or maybe the internals devs themselves don't know yet.
Sorry, I meant a misquote by the manual authors :wink:. I used misquote in the wrong context even then though :roll:.

As far as I am aware, $GLOBALS has never been deprecated. It has several advantages to using the global keyword outside the scope of a single function, include manipulation of all Globals as an array, copying by value, etc. I don't use globals much anymore outside a few legacy apps that need maintaining but I would suspect the global keyword would get deprecated long before $GLOBALS array does...
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I think I knew all those already. Probably coz I sit here and read the manual for at least a couple of hours a week.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I knew most of those. Have used a few here and there, though nothing regularly. I have never used bit offsets (C shifting) or continue/break with multiple parameters, but I knew they took them.

And I would agree, this place is an awesome place to learn new stuff all the time.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Little known PHP Idioms etc.

Post by jayshields »

ole wrote:I suddenly realised how much I wanted the Internet.
Happens to me too. At uni my internet got d/c'ed for about 30 hours and I was bored out of my skull. I ended up just sitting in someone elses room and distracting them from working the whole time.
ole wrote:[*]isset() - can take multiple parameters but only returns true if they are all set
Read that somewhere on here before, but I've not used it, though still interesting.
ole wrote:[*]global keyword is deprecated in favour of $GLOBALS
That could be true, but I can't find anything that backs it up, I think you can use either. I think some people think it should be the other way around because something similar got deprecated, but I can't think of what it is at the moment...
ole wrote:[*]break accepts an optional argument of the amount of nesting levels to break out of. Does anybody do this?
[*]continue also accepts an optional argument only this is the number of iterations to continue for
That's actually pretty interesting. Can't think of any situation where you would need that though. Probably be better to do the same thing a long winded way to increase readability.
ole wrote:However, you should really use the {} notation because it differentiates string offsets from array offsets and thus, makes your code more readable.
I agree with that.
ole wrote:Also I never ever managed to actually find a use for do..while
I never knew when they'd be used either, but after learning Java at uni it came to me a little bit, although I can only see myself rarely using one.

Example:

Code: Select all

do
{
  echo 'login details input form';
} while(/*login details are incorrect*/);

echo 'you\'re logged in!';
That's easier to read and less code than:

Code: Select all

while(/*login details are set and login details are incorrect*/)
{
  echo 'login details input form';
}

echo 'you\'re logged in!';
The above is what I used to do when I first started coding PHP.

Nice thread btw, enjoyed it :bow:
Post Reply