On Sat, 2009-03-07 at 13:42 +0000, Nathan Rixham wrote: > Ashley Sheridan wrote: > > On Thu, 2009-03-05 at 19:58 -0800, Michael A. Peters wrote: > >> PJ wrote: > >>> Nathan Rixham wrote: > >>>> Chris wrote: > >>>>> PJ wrote: > >>>>>> And again, this works: > >>>>>> if (strlen($_POST["first_nameIN"]) == 0 ) { > >>>>>> $obligatoryFieldNotPresent = 1; ... > >>>>>> > >>>>>> this does not: > >>>>>> > >>>>>> if (strlen($_POST["first_nameIN"]) > 0 ) && > >>>>>> (strlen($_POST["last_nameIN"]) > 0 ) { echo $first_nameIN, " ", > >>>>>> $last_nameIN); > >>>>>> else (echo "error";)} > >>>>>> > >>>>>> But, $first_nameIn and $last_nameIN do echo their contents without the > >>>>>> if clause and we see that the first if clause does work... what am I > >>>>>> doing wrong, again? > >>>>> Firstly please learn to indent your code (I don't care if this is > >>>>> just an example of your code, make it easier for everyone who's > >>>>> trying to help you). It's much easier to read this: > >>>>> > >>>>> if (strlen($_POST["first_nameIN"]) > 0 ) && > >>>>> (strlen($_POST["last_nameIN"]) > 0 ) { > >>>>> echo $first_nameIN, " ", $last_nameIN; > >>>>> } else { > >>>>> echo "error"; > >>>>> } > >>>>> > >>>>> If you want help we're not going to spend a long time reformatting > >>>>> your code to try and understand it. > >>>>> > >>>>> Now to the problem. > >>>>> > >>>>> Are both first_nameIN AND last_nameIN longer than 0 chars? > >>>>> > >>>>> var_dump($_POST['first_nameIN']); > >>>>> var_dump($_POST['last_nameIN']); > >>>>> > >>>>> maybe you only filled in first_name or last_name but not both. > >>>>> > >>>> syntax.. your brackets are all over the place > >>>> > >>>> your wrote: > >>>> > >>>> if (strlen($_POST["first_nameIN"]) > 0 ) && > >>>> (strlen($_POST["last_nameIN"]) > 0 ) { > >>>> echo $first_nameIN, " ", $last_nameIN; > >>>> } else { > >>>> echo "error"; > >>>> } > >>>> > >>>> should be: > >>>> > >>>> if( (strlen($_POST["first_nameIN"]) > 0) && > >>>> (strlen($_POST["last_nameIN"]) > 0) ) { > >>>> echo $first_nameIN, " ", $last_nameIN; > >>>> } else { > >>>> echo "error"; > >>>> } > >>>> > >>>> weirdly spaced but easier to read version *throws coding standards out > >>>> the window*: > >>>> > >>>> if( > >>>> (strlen($_POST["first_nameIN"]) > 0) > >>>> && > >>>> (strlen($_POST["last_nameIN"]) > 0) > >>>> ) > >>>> { > >>>> echo $first_nameIN, " ", $last_nameIN; > >>>> } > >>>> else > >>>> { > >>>> echo "error"; > >>>> } > >>>> > >>>> regards > >>>> > >>> Oooops, so it was only the parentheses... =-O > >>> Thanks, it works now. I do find it confusing just when and where to put > >>> the brackets... > >>> > >> I personally like to do an indent of 3 spaces and indent the closing > >> bracket. > >> > >> IE > >> > >> while ($foo != $bar) { > >> dosomestuff(); > >> dosomemorestuff(); > >> if ($frog > $ape) { > >> dosomeotherstuff(); > >> } elseif ($lizard < $cat) { > >> someaction(); > >> } else { > >> $bar++; > >> } > >> $foo++; > >> } > >> > >> Most don't do it that - they use line breaks before the open bracket etc. > >> > >> I don't remember where I learned it, looking at stuff I did in bash and > >> expect (tcl) and perl way back when, I didn't do it that way - but it > >> makes the most sense to me - because a closing bracket finishes the > >> statement that started on the line the opening bracket is on. > >> > >> I guess to each his own. > >> > >> It may have been some fancy text editor I tried that did it the way > >> automagically, or someone elses code I had to read, but damned if I > >> remember. > >> > > I'm more a fan of lining up opening and closing brackets so they are at > > the same indent level. It prevents one of the most popular errors caused > > by omitting a bracket, brace or other in the wrong place. A few extra > > line breaks in the code are not going to make a noticeable impact on the > > file size of the script! > > > > > > Ash > > www.ashleysheridan.co.uk > > > > > > yeah I do that with functions, methods, classes but not so much with > if/for/while/switch statements.. > > here's the coding standards I always break (when compared against zend) > > Private member variable must contain a leading underscore > > Expected "if (...) {\n"; found "if(...) {\n" > Expected "} elseif (...) {\n"; found "} elseif(...) {\n" > > Expected "foreach (...) {\n"; found "foreach(...) {\n" > > Space found before comma in function call > Space after opening parenthesis of function call prohibited > Space before closing parenthesis of function call prohibited > > but all of them are because I personally find > > if( $x < $y ) { > > } elseif( $x == $y ) { > > } > > more readable than: > > if ($x < $y) { > > } elseif ($x == $y) { > > } > > and > public function someMethod( $arg0 , $arg1 , $arg2 ) > { > > more readable than: > public function someMethod($arg0, $arg1, $arg2) > { > > regards, > nath It's the if/for/while's that I often have these problems with! For me, doing a switch/case like this takes only a tiny bit more time than how I've seen others do it, but has major benefits when code spans out into several screens worth:
switch($action) { case 'addCat': { // code break; } case 'editCat': { // code break; } case 'killCat': { // code break; } } Etc... I think some of the old style of coding where as little vertical space is used as possible is a throwback from the days when program listings were printed out for review afterwards, and found in magazines/books for hobbyists to type in (I am going back a bit now!) I do exactly the same with my HTML too, indenting block-level elements so that everything is readable with a quick glance afterwards (how many times have you forgot to close a div, or closed too many?) I think there's a lot to be said for good indenting practices. Ash www.ashleysheridan.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php