php-general Digest 26 Oct 2006 08:04:34 -0000 Issue 4422

Topics (messages 243622 through 243643):

Re: Problem with EXEC and PASSTHRU
        243622 by: Matt Beechey
        243642 by: Roman Neuhauser

Re: How does the Zend engine behave?
        243623 by: Jon Anderson
        243638 by: Larry Garfield

Re: exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);
        243624 by: Gert Cuykens
        243625 by: David Giragosian
        243626 by: Gert Cuykens
        243627 by: Stut
        243628 by: David Giragosian
        243629 by: Gert Cuykens
        243630 by: Stut
        243631 by: Gert Cuykens

<OPTION
        243632 by: Ron Piggott (PHP)
        243633 by: Stut
        243635 by: Joe Wollard
        243636 by: Paul Novitski
        243637 by: Robert Cummings

Re: foreach on a 3d array
        243634 by: M.Sokolewicz

Job Opening
        243639 by: Larry Garfield

heredoc usage [WAS: <OPTION]
        243640 by: Paul Novitski
        243641 by: Robert Cummings
        243643 by: Paul Novitski

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [email protected]


----------------------------------------------------------------------
--- Begin Message ---
Ok - This is a reply to ALL - Thanks for the great help - I now understand 
why it wasn't displaying - it was all due to caching of data at the client 
end before displaying in blocks. With some help in the comments of the php 
manual for flush() I found a nice script that uses echo str_pad('',1024); to 
help pad things out - also displays a little countdown to keep people amused 
and convinced something is happening while the service restarts! I'm LOVING 
php now - its a very simple language limited only by your imagination as to 
how you use it! My knowledge seems to be lacking more in HTML and Browser 
server relationships - I'm basing things on my background in Basic 
programming as a kid - you do Print "Hello world" and it does that instantly 
to the screen - little different with browsers!

Matt

"Myron Turner" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Matt Beechey wrote:
>> I am writing a php website for users to edit a global whitelist for Spam
>> Assassin - all is going fairly well considering I hadn't ever used php 
>> until
>> a couple of days ago. My problem is I need to restart AMAVISD-NEW after 
>> the
>> user saves the changes. I've acheived this using SUDO and giving the
>> www-data users the rights to SUDO amavisd-new. My problem is simply a 
>> user
>> friendlyness issue - below is the code I'm running -
>>
>> if(isset($_POST["SAVE"]))
>> {
>> file_put_contents("/etc/spamassassin/whitelist.cf", 
>> $_SESSION[whitelist]);
>> $_SESSION[count]=0;
>> echo "Restarting the service.........</A></P>";
>> exec('sudo /usr/sbin/amavisd-new reload');
>> echo "Service was restarted...... Returning to the main page.";
>> sleep(4)
>> echo '<meta http-equiv="refresh" content="0;URL=index.php">';
>> }
>>
>> The problem is that the Restarting the Service dialogue doesn't get
>> displayed until AFTER the Service Restarts even though it appears before 
>> the
>> shell_exec command. I've tried exec and passthru and its always the 
>> same - I
>> want it to display the "Service was restarted" - wait for 4 seconds and 
>> then
>> redirect to the main page. Instead nothing happens on screen for the 
>> browser
>> user until the service has restarted at which point they are returned to
>> index.php - its as if the exec and the sleep and the refresh to index.php
>> are all kind of running concurently.
>>
>> Can someone PLEASE tell me what I'm doing wrong - or shed light on how I
>> should do this.
>>
>> Thanks,
>>
>> Matt
>
>
> -- 
> You have to keep in mind that you are dealing with the difference in speed 
> between something that is taking place on the sever and something that is 
> being transmitted over a network.  You don't make it clear whether this is 
> an in-house site or whether it is used by people at a distance.  If the 
> latter, what you describe is not surprising at all.
>
> I've never had a reason to look into how PHP sequences events when it 
> pumps out a web page, but it's not unusual for scripts in Perl, for 
> instance, to show delays in browser output when doing something on the 
> server.  One  consideration would be how long amavisd-new takes to reload.
>
> I'm not very well-informed about hacking and security, but it would seem 
> to me that you are taking a risk by giving users root privileges to 
> restart amavisd-new.
>
>
> _____________________
> Myron Turner
> http://www.mturner.org/XML_PullParser/ 

--- End Message ---
--- Begin Message ---
# [EMAIL PROTECTED] / 2006-10-26 08:46:27 +1300:
> I'm LOVING php now - its a very simple language limited only by your
> imagination as to how you use it! My knowledge seems to be lacking
> more in HTML and Browser server relationships - I'm basing things on
> my background in Basic programming as a kid - you do Print "Hello
> world" and it does that instantly to the screen - little different
> with browsers!
 
    It's a little different with network communication.

    Try programming something for the console using the CLI
    (Command-Line Interface) version of PHP, should be much less
    confusing.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

--- End Message ---
--- Begin Message --- Take this with a grain of salt. I develop with PHP, but I am not an internals guy...

[EMAIL PROTECTED] wrote:
Are the include files only compiled when execution hits them, or are all include files compiled when the script is first compiled, which would mean a cascade through all statically linked include files. By statically linked files I mean ones like "include ('bob.php')" - i.e the filename isn't in a variable.
Compiled when execution hits them. You can prove this by trying to conditionally include a file with a syntax error: if (false) include('script_with_syntax_error.php'); won't cause an error.

Secondly, are include files that are referenced, but not used, loaded into memory? I.e Are statically included files automatically loaded into memory at the start of a request? (Of course those where the name is variable can only be loaded once the name has been determined.) And when are they loaded into memory? When the instruction pointer hits the include? Or when the script is initially loaded?
If your include file is actually included, it will use memory. If it is not included because of some condition, then it won't use memory.
Are included files ever unloaded? For instance if I had 3 include files and no loops, once execution had passed from the first include file to the second, the engine might be able to unload the first file. Or at least the code, if not the data.
If you define a global variable in an included file and don't unset it anywhere, then it isn't automatically "unloaded", nor are function/class definitions unloaded when execution is finished.

Once you include a file, it isn't unloaded later though - even included files that have just executed statements (no definitions saved for later) seem to eat a little memory once, but it's so minimal that you wouldn't run into problems unless you were including many thousand files. Including the same file again doesn't eat further memory. I assume the eaten memory is for something to do with compilation or caching in the ZE.
Thirdly, I understand that when a request arrives, the script it requests is compiled before execution. Now suppose a second request arrives for the same script, from a different requester, am I right in assuming that the uncompiled form is loaded? I.e the script is tokenized for each request, and the compiled version is not loaded unless you have engine level caching installed - e.g. MMCache or Zend Optimiser.
I think that's correct. If you don't have an opcode cache, the script is compiled again for every request, regardless of who requests it.

IMO, you're probably better off with PECL/APC or eAccelerator rather than MMCache or Zend Optimizer. I use APC personally, and find it exceptional -> rock solid + fast. (eAccelerator had a slight performance edge for my app up until APC's most recent release, where APC now has a significant edge.)
Fourthly, am I right in understanding that scripts do NOT share memory, even for the portions that are simply instructions? That is, when the second request arrives, the script is loaded again in full. (As opposed to each request sharing the executed/compiled code, but holding data separately.)
Yep, I think that's also correct.
Fifthly, if a script takes 4MB, given point 4, does the webserver demand 8MB if it is simultaneously servicing 2 requests?
Yep. More usually with webserver/PHP overhead.
Lastly, are there differences in these behaviors for PHP4 and PHP5?
Significant differences between 4 and 5, but with regards to the above, I think they're more or less the same.
--- End Message ---
--- Begin Message ---
On Wednesday 25 October 2006 14:48, Jon Anderson wrote:
> Take this with a grain of salt. I develop with PHP, but I am not an
> internals guy...
>
> [EMAIL PROTECTED] wrote:
> > Are the include files only compiled when execution hits them, or are
> > all include files compiled when the script is first compiled, which
> > would mean a cascade through all statically linked include files. By
> > statically linked files I mean ones like "include ('bob.php')" - i.e
> > the filename isn't in a variable.
>
> Compiled when execution hits them. You can prove this by trying to
> conditionally include a file with a syntax error: if (false)
> include('script_with_syntax_error.php'); won't cause an error.
>
> > Secondly, are include files that are referenced, but not used, loaded
> > into memory? I.e Are statically included files automatically loaded
> > into memory at the start of a request? (Of course those where the name
> > is variable can only be loaded once the name has been determined.) And
> > when are they loaded into memory? When the instruction pointer hits
> > the include? Or when the script is initially loaded?
>
> If your include file is actually included, it will use memory. If it is
> not included because of some condition, then it won't use memory.
>
> > Are included files ever unloaded? For instance if I had 3 include
> > files and no loops, once execution had passed from the first include
> > file to the second, the engine might be able to unload the first file.
> > Or at least the code, if not the data.
>
> If you define a global variable in an included file and don't unset it
> anywhere, then it isn't automatically "unloaded", nor are function/class
> definitions unloaded when execution is finished.
>
> Once you include a file, it isn't unloaded later though - even included
> files that have just executed statements (no definitions saved for
> later) seem to eat a little memory once, but it's so minimal that you
> wouldn't run into problems unless you were including many thousand
> files. Including the same file again doesn't eat further memory. I
> assume the eaten memory is for something to do with compilation or
> caching in the ZE.
>
> > Thirdly, I understand that when a request arrives, the script it
> > requests is compiled before execution. Now suppose a second request
> > arrives for the same script, from a different requester, am I right in
> > assuming that the uncompiled form is loaded? I.e the script is
> > tokenized for each request, and the compiled version is not loaded
> > unless you have engine level caching installed - e.g. MMCache or Zend
> > Optimiser.
>
> I think that's correct. If you don't have an opcode cache, the script is
> compiled again for every request, regardless of who requests it.
>
> IMO, you're probably better off with PECL/APC or eAccelerator rather
> than MMCache or Zend Optimizer. I use APC personally, and find it
> exceptional -> rock solid + fast. (eAccelerator had a slight performance
> edge for my app up until APC's most recent release, where APC now has a
> significant edge.)
>
> > Fourthly, am I right in understanding that scripts do NOT share

> > Fifthly, if a script takes 4MB, given point 4, does the webserver
> > demand 8MB if it is simultaneously servicing 2 requests?
>
> Yep. More usually with webserver/PHP overhead.

Unless you're using an opcode cache, as I believe some of the are smarter 
about that.  Which and how, I don't know. :-)

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

--- End Message ---
--- Begin Message ---
actually something like 'asdadsuiashdiasdh' :) test.php is just some
text file i maybe should have renamed it to test.txt sorry.

But the thing is i dont get any feedback like 'cant connect to host'
or 'invalid sql' etc.

exec(mysqldump...) works but exec(mysql...) doesnt

On 10/25/06, David Giragosian <[EMAIL PROTECTED]> wrote:
What is the SQL you are running in test.php?

David


On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:
>
> i do not get any output from mysql except form echo $bin that displays 1 ?
> <?php
> exec("mysql -h hhh -u uuu -pppp < test.php",$out,$bin);
> print_r($out);
> echo $bin;
> ?>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>



--- End Message ---
--- Begin Message ---
What do you get if you run:

echo exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);




On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:

actually something like 'asdadsuiashdiasdh' :) test.php is just some
text file i maybe should have renamed it to test.txt sorry.

But the thing is i dont get any feedback like 'cant connect to host'
or 'invalid sql' etc.

exec(mysqldump...) works but exec(mysql...) doesnt

On 10/25/06, David Giragosian <[EMAIL PROTECTED]> wrote:
> What is the SQL you are running in test.php?
>
> David
>
>
> On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:
> >
> > i do not get any output from mysql except form echo $bin that displays
1 ?
> > <?php
> > exec("mysql -h hhh -u uuu -pppp < test.php",$out,$bin);
> > print_r($out);
> > echo $bin;
> > ?>
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
>
>


--- End Message ---
--- Begin Message ---
when i do echo exec... i get a blank screen

(print_r $out i get Array() echo $bin i get 1)

On 10/25/06, David Giragosian <[EMAIL PROTECTED]> wrote:
What do you get if you run:

echo exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);





On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:
> actually something like 'asdadsuiashdiasdh' :) test.php is just some
> text file i maybe should have renamed it to test.txt sorry.
>
> But the thing is i dont get any feedback like 'cant connect to host'
> or 'invalid sql' etc.
>
> exec(mysqldump...) works but exec(mysql...) doesnt
>
> On 10/25/06, David Giragosian < [EMAIL PROTECTED]> wrote:
> > What is the SQL you are running in test.php?
> >
> > David
> >
> >
> > On 10/25/06, Gert Cuykens <[EMAIL PROTECTED] > wrote:
> > >
> > > i do not get any output from mysql except form echo $bin that displays
1 ?
> > > <?php
> > > exec("mysql -h hhh -u uuu -pppp < test.php",$out,$bin);
> > > print_r($out);
> > > echo $bin;
> > > ?>
> > >
> > > --
> > > PHP General Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> > >
> >
> >
>



--- End Message ---
--- Begin Message ---
Gert Cuykens wrote:
i do not get any output from mysql except form echo $bin that displays 1 ?
<?php
exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);
print_r($out);
echo $bin;
?>

I don't know for sure but chances are that mysql outputs errors on stderr not stdout, so you need to redirect stderr to stdout for exec to capture it...

exec("mysql -h hhh -u uuu -pppp < test.php 2>&1",$out,$bin);

-Stut

--- End Message ---
--- Begin Message ---
Can't say why you're not getting an error message, but when I have a valid
user, password, host, and sql in a .txt file, I get the last record from the
select statement I run output in the browser. Maybe start with getting it to
work with everything used correctly, then work backwards.

HTH,


On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:

when i do echo exec... i get a blank screen

(print_r $out i get Array() echo $bin i get 1)

On 10/25/06, David Giragosian <[EMAIL PROTECTED]> wrote:
> What do you get if you run:
>
> echo exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);
>
>
>
>
>
> On 10/25/06, Gert Cuykens <[EMAIL PROTECTED]> wrote:
> > actually something like 'asdadsuiashdiasdh' :) test.php is just some
> > text file i maybe should have renamed it to test.txt sorry.
> >
> > But the thing is i dont get any feedback like 'cant connect to host'
> > or 'invalid sql' etc.
> >
> > exec(mysqldump...) works but exec(mysql...) doesnt
> >
> > On 10/25/06, David Giragosian < [EMAIL PROTECTED]> wrote:
> > > What is the SQL you are running in test.php?
> > >
> > > David
> > >
> > >
> > > On 10/25/06, Gert Cuykens <[EMAIL PROTECTED] > wrote:
> > > >
> > > > i do not get any output from mysql except form echo $bin that
displays
> 1 ?
> > > > <?php
> > > > exec("mysql -h hhh -u uuu -pppp < test.php",$out,$bin);
> > > > print_r($out);
> > > > echo $bin;
> > > > ?>
> > > >
> > > > --
> > > > PHP General Mailing List (http://www.php.net/)
> > > > To unsubscribe, visit: http://www.php.net/unsub.php
> > > >
> > > >
> > >
> > >
> >
>
>


--- End Message ---
--- Begin Message ---
BINGO :)

ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1)Array ( [0] =>
ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1) ) 1

Can you explain a bit more what this do ? 2>&1 For example will i only
get stderr or do i get stdout and stderr messages ?

On 10/25/06, Stut <[EMAIL PROTECTED]> wrote:
Gert Cuykens wrote:
> i do not get any output from mysql except form echo $bin that displays 1 ?
> <?php
> exec("mysql -h hhh -u uuu -pppp <test.php",$out,$bin);
> print_r($out);
> echo $bin;
> ?>

I don't know for sure but chances are that mysql outputs errors on
stderr not stdout, so you need to redirect stderr to stdout for exec to
capture it...

exec("mysql -h hhh -u uuu -pppp < test.php 2>&1",$out,$bin);

-Stut


--- End Message ---
--- Begin Message ---
Gert Cuykens wrote:
BINGO :)

Indeed.

ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1)Array ( [0] =>
ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1) ) 1

Can you explain a bit more what this do ? 2>&1 For example will i only
get stderr or do i get stdout and stderr messages ?

The 2>&1 syntax redirects stderr to stdout, it does not do anything with stdout. So yes, you will still also get stdout messages.

This no longer has anything to do with PHP. I suggest you Google for info on Linux pipes.

-Stut

--- End Message ---
--- Begin Message ---
ok thx all, works great now :)

On 10/25/06, Stut <[EMAIL PROTECTED]> wrote:
Gert Cuykens wrote:
> BINGO :)

Indeed.

> ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1)Array ( [0] =>
> ERROR 2005 (HY000): Unknown MySQL server host 'hhh' (1) ) 1
>
> Can you explain a bit more what this do ? 2>&1 For example will i only
> get stderr or do i get stdout and stderr messages ?

The 2>&1 syntax redirects stderr to stdout, it does not do anything with
stdout. So yes, you will still also get stdout messages.

This no longer has anything to do with PHP. I suggest you Google for
info on Linux pipes.

-Stut


--- End Message ---
--- Begin Message ---
I am creating a form right now.  I am using the html SELECT tag.

<SELECT NAME="day_of_month">

The most number of days in a month is 31

I want the output to be <OPTION SELECTED>## based on the value of
$selected_day_of_month variable.  For the days of the month where the
number is not selected the output I am desiring is 

<OPTION>1
<OPTION>2
etc.

to be generated through PHP code

I already have a value the user submitted in a variable named
$selected_day_of_month from being stored in a mySQL table

Does anyone know how to do this in a few commands?  I am wanting to
simplify 

<?
if ( $selected_day_of_month == "1" ) }
<OPTION SELECTED>1
} ELSE {
<OPTION>1
}

if ( $selected_day_of_month == "2" ) }
<OPTION SELECTED>2
} ELSE {
<OPTION>2
}

?>

etc.

Ron


--- End Message ---
--- Begin Message ---
Ron Piggott (PHP) wrote:
I am creating a form right now.  I am using the html SELECT tag.

<SELECT NAME="day_of_month">

The most number of days in a month is 31

I want the output to be <OPTION SELECTED>## based on the value of
$selected_day_of_month variable.  For the days of the month where the
number is not selected the output I am desiring is
<OPTION>1
<OPTION>2
etc.

to be generated through PHP code

I already have a value the user submitted in a variable named
$selected_day_of_month from being stored in a mySQL table

Does anyone know how to do this in a few commands?  I am wanting to
simplify
<?
if ( $selected_day_of_month == "1" ) }
<OPTION SELECTED>1
} ELSE {
<OPTION>1
}

if ( $selected_day_of_month == "2" ) }
<OPTION SELECTED>2
} ELSE {
<OPTION>2
}

?>

etc.

Dang that's painful!! Try this...

<?php
    foreach (range(1, 31) as $day)
    {
        print '<option value="'.$day.'"';
        if ($selected_day_of_month == $day)
            print ' selected';
        print '>'.$day.'</option>';
    }

?>

I added a value to the options (you knew that was missing right?). Obviously you need to output the <select ...> and </select> outside this loop.

-Stut

--- End Message ---
--- Begin Message ---
...and if you wanted to go an extra step you could try an alternate
syntax of the exact same code:

<?php
   foreach (range(1, 31) as $day)
       print '<option value="' . $day . '"' . ($selected_day_of_month
== $day ? ' selected' : '') . '>' . $day . '</option>';
?>

Either way, Stut is correct. Don't type this out 31 times unless you
really really really like to type. ;-)
- Joe

On 10/25/06, Stut <[EMAIL PROTECTED]> wrote:
Ron Piggott (PHP) wrote:
> I am creating a form right now.  I am using the html SELECT tag.
>
> <SELECT NAME="day_of_month">
>
> The most number of days in a month is 31
>
> I want the output to be <OPTION SELECTED>## based on the value of
> $selected_day_of_month variable.  For the days of the month where the
> number is not selected the output I am desiring is
>
> <OPTION>1
> <OPTION>2
> etc.
>
> to be generated through PHP code
>
> I already have a value the user submitted in a variable named
> $selected_day_of_month from being stored in a mySQL table
>
> Does anyone know how to do this in a few commands?  I am wanting to
> simplify
>
> <?
> if ( $selected_day_of_month == "1" ) }
> <OPTION SELECTED>1
> } ELSE {
> <OPTION>1
> }
>
> if ( $selected_day_of_month == "2" ) }
> <OPTION SELECTED>2
> } ELSE {
> <OPTION>2
> }
>
> ?>
>
> etc.

Dang that's painful!! Try this...

<?php
     foreach (range(1, 31) as $day)
     {
         print '<option value="'.$day.'"';
         if ($selected_day_of_month == $day)
             print ' selected';
         print '>'.$day.'</option>';
     }

?>

I added a value to the options (you knew that was missing right?).
Obviously you need to output the <select ...> and </select> outside this
loop.

-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



--- End Message ---
--- Begin Message ---
At 10/25/2006 04:09 PM, Stut wrote:
Dang that's painful!! Try this...

<?php
    foreach (range(1, 31) as $day)
    {
        print '<option value="'.$day.'"';
        if ($selected_day_of_month == $day)
            print ' selected';
        print '>'.$day.'</option>';
    }

?>


Ouch!  Gnarly mix of logic and markup.  I suggest something more like:

        foreach (range(1, 31) as $day)
        {
$sSelected = ($selected_day_of_month == $day) ? ' selected="selected"' : '';

                print <<< hdDay
                <option value="$day"$sSelected>$day</option>

hdDay;
        }

--- End Message ---
--- Begin Message ---
On Wed, 2006-10-25 at 17:35 -0700, Paul Novitski wrote:
> At 10/25/2006 04:09 PM, Stut wrote:
> >Dang that's painful!! Try this...
> >
> ><?php
> >     foreach (range(1, 31) as $day)
> >     {
> >         print '<option value="'.$day.'"';
> >         if ($selected_day_of_month == $day)
> >             print ' selected';
> >         print '>'.$day.'</option>';
> >     }
> >
> >?>
> 
> 
> Ouch!  Gnarly mix of logic and markup.  I suggest something more like:
> 
>          foreach (range(1, 31) as $day)
>          {
>                  $sSelected = ($selected_day_of_month == $day) ? ' 
> selected="selected"' : '';
> 
>                  print <<< hdDay
>                  <option value="$day"$sSelected>$day</option>
> 
> hdDay;
>          }

Ewww, I'll take Stut's style anyday. Heredoc has its uses, but I
wouldn't consider your above usage one of them :/ Now to add my own
flavour...

<?php

    for( $day = 1; $day <= 31; $day++ )
    {
        $selected
            = $selected_day_of_month == $day
            ? ' selected="selected"'
            : '';

        echo '<option value="'.$day.'"'.$selected.'>'
            .$day
            .'</option>';
    }

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
M.Sokolewicz wrote:
Dotan Cohen wrote:
On 24/10/06, Chris Boget <[EMAIL PROTECTED]> wrote:
> $languages = array(
>    "af"  => array("Afrikaans", "Afrikaans", "South Africa"),
>    "sq"  => array("Albanian", "Shqipe", "Albania")        );
>
> foreach ($languages as $language){
>    if (  strstr( $_HTTP_ACCEPT_LANGUAGE, $language)  ) {
>        print"<center>You are from ".$language[2]."!</center>";
>    }
> }

What you want is something like this:

foreach ($languages as $language => $valueArray ){
   if (  strstr( $_HTTP_ACCEPT_LANGUAGE, $language)  ) {
       print"<center>You are from ".$valueArray[2]."!</center>";
   }
}

Your example is setting the variable $language to the array for each
iteration.

Thanks, I see what I was missing.

So the first iteration,

$language is set to array("Afrikaans", "Afrikaans", "South Africa")

and the second iteration,

$language is set to array("Albanian", "Shqipe", "Albania")

That much I knew. Thanks, Chris.

Dotan Cohen

http://essentialinux.com/
http://technology-sleuth.com/

Why not just do

if(isset($language[$_HTTP_ACCEPT_LANGUAGE])) {
print '<center>You are from '.$language[$_HTTP_ACCEPT_LANGUAGE][3].'!</center>';
} else {
    print '<center>where are you from?!</center>';
}

using strstr is pretty slow, and considering you made the indices correspond to the actual value you're checking em for... this is a lot faster ^^ (and cleaner IMO)

- tul

ofcourse, that should've read:
$languages = array(
        "af"  => array("Afrikaans", "Afrikaans", "South Africa"),
        "sq"  => array("Albanian", "Shqipe", "Albania")        );

if(isset($languages[$_HTTP_ACCEPT_LANGUAGE])) {
    print '<center>You are from
'.$language[$_HTTP_ACCEPT_LANGUAGE][3].'!</center>';
} else {
    print '<center>where are you from?!</center>';
}

--- End Message ---
--- Begin Message ---
Well since the consensus seemed to be to allow job postings, I'll make 
one. :-)

My company is looking for a few good PHP programmers.

We are a Chicago-area web consulting firm developing web sites and web 
applications for a variety of clients, including several large academic 
institutions.  We are looking for skilled PHP developers to join our team.  
(Sorry, that means yes, you'd have to work with me.)  It's a small but 
growing company of less than 10 people, all fairly young and geeky. :-)  
Experience with PHP (although not necessarily prior work experience) is a 
must.  

If interested, contact me OFF-LIST for more information.  Note: I am NOT in a 
hiring position, so do NOT send me your resume. :-)  I'm just going to pass 
you on to the person who is.

-- 
Larry Garfield                  AIM: LOLG42
[EMAIL PROTECTED]               ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson

--- End Message ---
--- Begin Message ---

> At 10/25/2006 04:09 PM, Stut wrote:
> >         print '<option value="'.$day.'"';
> >         if ($selected_day_of_month == $day)
> >             print ' selected';
> >         print '>'.$day.'</option>';

On Wed, 2006-10-25 at 17:35 -0700, Paul Novitski wrote:
>                  print <<< hdDay
>                  <option value="$day"$sSelected>$day</option>
>
> hdDay;

At 10/25/2006 06:57 PM, Robert Cummings wrote:
Ewww, I'll take Stut's style anyday. Heredoc has its uses, but I
wouldn't consider your above usage one of them :/ Now to add my own
flavour...
        echo '<option value="'.$day.'"'.$selected.'>'
            .$day
            .'</option>';


Rob, I'd love to know what aspect of my heredoc usage you find objectionable. From my perspective it's a single integrated expression, cleaner, easier to read and proofread, and easier to maintain than a concatenation of expression fragments strung together with syntactical punctuation and quotation marks. I especially value the fact that heredoc lets me separate program logic from text output to the greatest extent possible. Am I unwittingly committing a stylistic or logical faux pas?

Conversely, what applications of heredoc do you find valid and valuable?

I ask because I use heredoc all the time, sometimes for inline code as above but most often for template assembly, maintaining HTML in external files that can be edited independently of the PHP logic.

Curiously,
Paul
--- End Message ---
--- Begin Message ---
On Wed, 2006-10-25 at 22:53 -0700, Paul Novitski wrote:
> > > At 10/25/2006 04:09 PM, Stut wrote:
> > > >         print '<option value="'.$day.'"';
> > > >         if ($selected_day_of_month == $day)
> > > >             print ' selected';
> > > >         print '>'.$day.'</option>';
> 
> On Wed, 2006-10-25 at 17:35 -0700, Paul Novitski wrote:
> > >                  print <<< hdDay
> > >                  <option value="$day"$sSelected>$day</option>
> > >
> > > hdDay;
> 
> At 10/25/2006 06:57 PM, Robert Cummings wrote:
> >Ewww, I'll take Stut's style anyday. Heredoc has its uses, but I
> >wouldn't consider your above usage one of them :/ Now to add my own
> >flavour...
> >         echo '<option value="'.$day.'"'.$selected.'>'
> >             .$day
> >             .'</option>';
> 
> 
> Rob, I'd love to know what aspect of my heredoc usage you find 
> objectionable.

Now this is obviously just opinion since everyone is entitled to their
own style, and while I might find it objectionable, I hardly represent
the masses and their idiosyncrasies :)

Now, the thing that I dislike about heredoc for such small strings is
the switching between heredoc mode and the switching back. It's ugly on
the scale of switching in and out of PHP tags. it would be less ugly if
the closing delimiter didn't have to be at the beginning of it's own
line in which case code could still maintain indentation formatting.
Understandably it is not like this since it's flexibility lies in being
able to place very versatile content within while not accidentally
breaking out of heredoc context. To lighten the mess of heredoc I
personally use the following format:

<?php

    $foo = <<<_
        <div class="$foo" id="wooohooo">
            blah blah blah blah blah blah blah blah blah blah
            blah blah $foo blah blah blah blah blah blah blah
            blah blah blah blah blah blah $fee blah blah blah
            blah blah blah blah blah blah blah blah blah blah
        </div>
_;

?>
However that's only useful if indenting the content is not an issue.

>   From my perspective it's a single integrated 
> expression, cleaner, easier to read and proofread, and easier to 
> maintain than a concatenation of expression fragments strung together 
> with syntactical punctuation and quotation marks.  I especially value 
> the fact that heredoc lets me separate program logic from text output 
> to the greatest extent possible.  Am I unwittingly committing a 
> stylistic or logical faux pas?
> 
> Conversely, what applications of heredoc do you find valid and valuable?

My example also illustrates the kind of content I would be willing to
contain within a heredoc... namely a large chunk of content, most likely
with a mix of double quotes and variables. That said, I'm still more
likely not to use heredoc... I think the one exception where I'm quite
likely to use heredoc is when I'm declaring a javascript function from
within a PHP function.

> I ask because I use heredoc all the time, sometimes for inline code 
> as above but most often for template assembly, maintaining HTML in 
> external files that can be edited independently of the PHP logic.

I use a tag based template system, there's no PHP in my content so my
content files for the most part just look like more HTML. The templates
are compiled to PHP (or raw content such as when creating stylesheets)
after which they are used over and over without recompiling. here's an
example of an email template:

--------------------------------
Hello <jinn:render name="email" selector="firstName"/>,

You or somebody posing as you has requested that your password
be reset. If you did not request this email then you may disregard
it and no changes will be made to your account.

To continue please click on the link below and complete the form
that will be presented. If clicking on the link does not open
your browser then please copy and paste the address into a web
browser to continue.

    http://<jinn:render name="email"
selector="domain"/>/reset.php?rid=<jinn:render name="email"
selector="serialCode"/>

Sincerely,
Admin
--------------------------------

Here's the code that invokes the template:

<?php

    $mail = &$this->getServiceRef( 'mail' );

    $mail->addTo( $to );
    $mail->setSubject( 'BlahBlah.com - Reset Password Request' );
    $mail->setTemplate( '//emails/passwordResetDetails.php', $data );
    $mail->send();

?>

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

--- End Message ---
--- Begin Message ---
At 10/25/2006 11:24 PM, Robert Cummings wrote:
Now, the thing that I dislike about heredoc for such small strings is
the switching between heredoc mode and the switching back. It's ugly on
the scale of switching in and out of PHP tags.

It's so interesting that some details bug some people and others bug others. In an expression such as this:

echo '<option value="' . $day . '"' . $selected . '>' . $day . '</option>';

...I count eight transitions (instances of switching between PHP syntax and output text) -- one each time you open or close a quote -- compared to just two transitions (the opening & closing labels) for the comparable heredoc statement:

        print <<< hdDay
        <option value="$day"$sSelected>$day</option>

hdDay;


it would be less ugly if
the closing delimiter didn't have to be at the beginning of it's own
line in which case code could still maintain indentation formatting.

I agree. Too bad the PHP developers didn't come up with a special symbol for heredoc closing labels to give us more control over script formatting. I do however find this a fairly minor irritation, a small price to pay for heredoc's power.


To lighten the mess of heredoc I
personally use the following format:

    $foo = <<<_
        ...
_;

Nice and concise!  Thanks, I'll use that.


However that's only useful if indenting the content is not an issue.

I don't see how you draw that conclusion. Heredoc doesn't become useless if we need to indent the content, it just means we lose complete control over indenting every line of our PHP script. The functionality is still totally there.

This script indenting issue with heredoc is for me one of the few irritations of PHP. I love the language and the power that heredoc gives me. I'm also irritated by the class member syntax $this->that but I still use it as well.


> I ask because I use heredoc all the time, sometimes for inline code
> as above but most often for template assembly, maintaining HTML in
> external files that can be edited independently of the PHP logic.

I use a tag based template system, there's no PHP in my content so my
content files for the most part just look like more HTML.

This is a different topic, but also one close to my heart. Yes, I too use my own selector-based templating system. It does allow me to embed PHP variables into the template if I want, but the primary merge between plain HTML template and MySQL content happens through CSS-style selectors.


Hello <jinn:render name="email" selector="firstName"/>,

Cool.

My comparable example (but in an HTML context) would look like:

        Hello <span class="firstName">FIRSTNAME</span>,

where the engine replaces the content of the span with the value from the database based on a match of 'span.firstName' or perhaps just '.firstName'. (In this example the 'FIRSTNAME' content in the template is merely a place-holder to make it easier to preview the markup and isn't necessary to the merge process.)


| InterJinn Application Framework - http://www.interjinn.com |

Interesting project!  Good work.

Paul
--- End Message ---

Reply via email to