Daevid Vincent wrote:
>
>
>> -----Original Message-----
>> Daevid Vincent wrote:
>>> NO! For the love of God and all that is holy, don't do that
>> accumulator / mod "hack".
>>> That's sooooo 1980's. And why make the CPU do all that math
>> for every row...
>>> Just do this. It's quick and simple:
>>>
>>> CSS:
>>> .dataRow1 { background-color: #DFDFDF; }
>>> .dataRow2 { background-color: #FFFFFF; }
>>>
>>> foreach ($foo_array as $foo) {
>>> ?><tr class="<?= ($dr = !$dr) ? "dataRow1" : "dataRow2"
>> ?>"><td><?= $foo
>>> ?></td></tr><?php
>> Wow, were to start with all the problems in the above code:
>> 1. Short tags?
>
> Nothing wrong with them. Do yourself a test. There is zero speed difference
> in a page. It makes your code cleaner and easier to read, plus less
> kilobytes you have to pull from the hard drive, therefore faster pages.
>
> http://stackoverflow.com/questions/662891/is-there-a-speed-difference-betwee
> n-php-echo-var-and-var
>
> http://cubicspot.blogspot.com/2009/06/maximum-failure-php-6-deprecates-short
> .html
>
Looks like all your code is going to stop working when PHP 6 comes
around. Since the "<?= " is part of the family they look to deprecate.
(read next comment)
> Are you sure you're not confused with "short_open_tags" ie. <? ?> -- which I
> am fully against (as am I against <% %> too), although sadly they are lumped
> into the same directive, when IMHO they should be separate.
>
> http://us.php.net/manual/en/ini.core.php#ini.short-open-tag
>
> http://www.php.net/~derick/meeting-notes.html#remove-support-for-and-script-
> language-php-and-add-php-var
>
Glad you mention this. If you look at the note for the short-open-tag
option, it says:
Note: This directive also affects the shorthand "<?= ", which is
identical to "<? echo ". Use of this shortcut requires short_open_tag
to be on.
So, you are saying you do not like to promote the use of short_open_tag,
but in your examples of how to write your code, you use them because it
shortens your code, etc...
So, which is it, do you like using short_open_tag or not??? Because
"<?=" is part of the short_open_tag family
>> 2. Not using echo or print.
>
> <?= ?> is a fantastic shortcut to the antiquated <?php echo ""; ?> B.S.
Yours requires short_open_tag to be on and uses them, mine doesn't
Plus, I would never do this in side of HTML to begin with. If I was in
the middle of a script, then I would have PHP echo the entire HTML
portion of the code rather then breaking out of PHP - do some HTML -
break in to PHP and echo a variable - break out and finish the HTML.
> The vast majority of your page is output in little fragments like this, so
> why not keep it clean and easy to read. Why would you purposely choose to be
> more verbose than you need to be for something as basic as "print".
>
>> You actually recommend breaking in/out of php?
>
> Hell yeah I do. THAT is one of the MAIN reasons to use PHP. Otherwise, why
> not just render your whole page with a bunch of $html .= '<tr><td>...';
> tags and print $html at the end. Welcome to the year 2000 my friend.
>
So, your example above would tell me that you are the type of guy that
would echo all your HTML code from within your nested php functions? hmmm
Breaking in/out of PHP all the time, what a mess of code that would be.
I have tried tasting that soup before. I didn't like it...
> You're probably the same kind of person that does this crap:
>
> if ($foo == true)
> {
> echo "A";
> }
> else
> {
> echo "B";
> }
>
Actually, my crap would look like this:
if ( $foo ) {
echo 'A';
} else {
echo 'B';
}
No double quotes (no variables need to be worked with)
Open curly bracket on the end of the previous line
I actually wrote a script that will take your example and turn it
into my crap
> (complete with extra braces and checking for 'true' explicitly, but not
> using === in such cases)
>
I do know the difference between == and ===
> Rather than a much cleaner, easier to read/write and more concise:
>
> <?= ($foo) ? 'A' : 'B' ?>
>
Again with your short_open_tag recommendations... When will you learn
that this is going against what you preach.
>
>> 3. Using uninitialized variables (I run with the E_ALL crowd)
>
> I'm so happy for you. Do you have a membership card and everything? That's
> your own masochistic fault then. I run with the
> save-myself-the-headache-and-write-clean-efficient-code crowd.
>
I don't think I am causing myself any pain.
clean !== use uninitialized variables
efficient !== squelching the NOTICE(s) that get generated by your code
that uses uninitialized variables. Not having them be generate in the
first place would be better.
>> 4. Using the <tr class=''> and not the <td class=''>... Hmmm
>
> Maybe you're new to how HTML works, but if you want to highlight a ROW (as
> the OP did), then you put the background color on the highest parent that it
> applies to. Hey imagine that, there is a TR tag which stands for TABLE ROW
> tag. Seems obvious to me. This reduces your page size in kilobytes, makes a
> much cleaner HTML rendering to read in source, and is the PROPER way to do
> it.
>
I have found, in a number of cases, that using only the <TR > tag
doesn't work all the time.
>>> }
>>>
>>> No need to initialize $dr as by default PHP will make it a
>> boolean "false",
>>> then each itteration, it will toggle true/false and
>> substitute the CSS class
>>>> -----Original Message-----
>>>> From: Jim Lucas [mailto:[email protected]]
>>>> Sent: Monday, August 10, 2009 4:03 PM
>>>> To: John Butler
>>>> Cc: PHP-General List
>>>> Subject: Re: [PHP] how to say "inverse your value" (to a boolean)?
>>>>
>>>> John Butler wrote:
>>>>> quick Q:
>>>>> I have this inside a foreach{} that I want to alternate
>>>> between on and
>>>>> off so I can alternate the background-color of my <tr>'s.
>>>>>
>>>>> $tableRowBGcolorBoolCounter != $tableRowBGcolorBoolCounter;
>>>> //-boolean
>>>>> on and off
>>>>>
>>>>> I am looking thru' docs and books, but can't remember (nor
>>>> find now) in
>>>>> PHP how to say "inverse your value" (to a boolean).
>>>>> ?
>>>>>
>>>>> TIA! -G
>>>>>
>>>>>
>>>> <?php
>>>>
>>>> $arr = range(1, 10);
>>>>
>>>> $i = 0;
>>>> foreach ( $arr AS $row ) {
>>>>
>>>> $row_color = ( ( $i++ % 2 ) ? 'green' : 'red');
>>>>
>>>> echo $row_color;
>>>>
>>>> }
>>>>
>>>> ?>
>>>>
>> another (neat|strange)+ way I use the above is like so
>>
>> <style>
>> .rowColor0 { background: #FFFFFF; }
>> .rowColor1,
>> .rowColor3 { background: #EEEEEE; }
>> .rowColor2 { background: #DDDDDD; }
>> </style>
>> <?php
>>
>> $arr = range(1, 10);
>>
>> $i = 0;
>>
>> foreach ( $arr AS $row ) {
>>
>> echo '<tr><td class="rowColor'.( $i++ % 4
>> ).'"'>'.$row.'</td></tr>';
>>
>> }
>> ?>
>>
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>
>>
>> --
>> Jim Lucas
>>
>> "Some men are born to greatness, some achieve greatness,
>> and some have greatness thrust upon them."
>>
>> Twelfth Night, Act II, Scene V
>> by William Shakespeare
>>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php