TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-22-2008, 05:07 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default Breaking out of a... not a loop.

Is there any way, say if you were using curly braces to organize your code into blocks, ie;

PHP Code:
function somecall() {
$var "somevar";

    {
     if (
$then) { do(); }
     while (!
$crashed) { crash(); }
    }


can you break out of those blocks like you can break out of a loop? Dependent of course on something evaluating to true, say halfway through the block there's an if statement that checks a value and if it's true, I don't want to process the rest of the block.

This is for purely aesthetic reasons at this point, I'm trying to create a block of if statements that will look more like a switch statement than a spaghetti mess of if's and else if's.

PHP Code:
{
  if (empty(
$value)) { dothis(); break; }
  if (!
ctype_alpha($value)) { dothis(); break; }
  if (
strcasecmp($value$othervalue)) { dothis(); break; }

-m
delayedinsanity is offline  
Reply With Quote
Old 04-22-2008, 06:22 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Within a function you can return at any point.
PHP Code:
function check($value)
{
  if (empty(
$value)) { dothis(); return; }
  if (!
ctype_alpha($value)) { dothis(); return; }
  if (
strcasecmp($value$othervalue)) { dothis(); return; }

As for otherwise, what's the harm in using elseif to string your checks together? In other words, why is this (bearing in mind, it's not syntactically correct PHP):
PHP Code:
{
  if (empty(
$value)) { dothis(); break; }
  if (!
ctype_alpha($value)) { dothis(); break; }
  if (
strcasecmp($value$othervalue)) { dothis(); break; }

any prettier than:
PHP Code:
if     (empty($value))                   dothis();
elseif (!
ctype_alpha($value))            dothat();
elseif (
strcasecmp($value$othervalue)) dothose(); 
If you absolutely insist in using break then you'll need to use some form of looping structure (to loop once) out of which you can break (note: not pretty!) :

PHP Code:
$foo 'car';
do {
    if (
$foo === 'aar') { echo 'aar'; break; }
    if (
$foo === 'bar') { echo 'bar'; break; }
    if (
$foo === 'car') { echo 'car'; break; }
    if (
$foo === 'dar') { echo 'dar'; break; }
    echo 
'ear';
} while (
0); 
Salathe is offline  
Reply With Quote
Old 04-22-2008, 06:30 PM   #3 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

Yeah, in retrospect I jumped the gun asking that question. I know break wasn't going to work, I had already tested that one. I had a huge block of else if's and I was just looking at different ways of writing it so that I could still read it later on. Turns out using curly braces to block out each specific group and using comments to label them made it easily readable on its own.

I just think too much, and usually not about useful stuff. Thanks anyways. :)
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:46 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design