[PHP] Re: Preferred Syntax
On Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote: >Can someone tell me which of the following is preferred and why? > > echo "$page_name"; > > echo "".$page_name.""; >[...] Just to throw in yet another possibility: echo <<$page_name HTML; I love HEREDOC for slabs of HTML, sometimes SQL, email bodies, etc. because they allow you to drop your variables into the output text without crufting up the formatting with string concatenation, AND they allow you to use double quotes which can be important for HTML attributes that may contain single quotes. So whilst either above option is fine for the specific context, I prefer HEREDOC when there's attributes like href. But what is "preferred" is rather dependent on the "preferrer". -- Ross McKay, Toronto NSW Australia "All we are saying Is give peas a chance" - SeedSavers -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to use a variable variable with an array [solved]
Dear Robert, On 14.12.2011, at 22:02, Robert Cummings wrote: > $path = ‘/path/to/data/in/my/arbitrarily/deep/array/'; > $array = $someBigFatArray; > $focus = &$array; > foreach( explode( '/', $path ) as $step ) > { > if ($step === '' || $step === 'CONFIGURATION’) > { > continue; > } > if( isset( $focus[$step] ) ) > { > $focus = &$focus[$step]; > } > else > { > unset( $focus ); > $focus = null; > break; > } > } > echo ""; echo var_export($focus, TRUE); echo ""; This solution is excellent! I modified it a little to match for empty $step and then continue, just in case there is a leading, ending or doubled “/“ and same for CONFIGURATION which will be my indicator to find the values where we expect a value replacement. if ($step === '' || $step === 'CONFIGURATION’) { continue; } Great thing! Thanks a lot to all! Cheers, Nils -- http://webint.cryptonode.de / a Fractal project -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to use a variable variable with an array
On 11-12-14 01:11 AM, Laruence wrote: On Wed, Dec 14, 2011 at 9:27 AM, Nils Leideck wrote: Hi Al, many thanks for your feedback. Unfortunately I don’t know the deepness of the array so I can’t use the nested foreach() idea :-( Let me try to simplify my question (which also helps myself to clarify my process ;-) I have a variable where the value is a string that is exactly the path to the array value: [code] $arrayPath = "['user_interface’][‘design']['my_colors']['item_number_one’]”; [/code] And I have an array that has all the details: [code] $myArray = coolFunction(‘getArray’); [/code] The question is, how can I use these both informations to return a value from the $myArray? Examples I tried unsuccessfully: [code] [...] echo $myArray$arrayPath; echo $myArray{$arrayPath}; echo $myArray${arrayPath}; echo $myArray${$arrayPath}; echo $myArray.$arrayPath; echo $myArray.$arrayPath; [...] [/code] etc... your feedback is much much much appreciated!!! Cheers, Nils -- http://webint.cryptonode.de / a Fractal project for you quesion, try eval: array("b"=>array("c"))); $key = "['a']'['b]"; $value = eval("return \$arr". $key .";"); ?> ps: your requirement is a little odd :) If the solution is to use eval() then you've almost certainly come up with the wrong answer. To get the desired answer you can either use recursion to recursively index the array as necessitated by the "path" or you can use references to incremententally step through the array. Example: You can wrap this puppy in a function too... and it will be faster than the recursive version someone else wrote because it doesn't incur the overhead of multiple function calls (for depths greater than 1). Recursion is great, recursion is elegant, computer science professors love recursion, but it can also be a resource pig and PHP doesn't do tail recursion :) Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to use a variable variable with an array
Hi FeIn and Laruence, On 14.12.2011, at 09:47, Nils Leideck wrote: >> function coolFunction( array $array, $search, $separator = ‘###' ) >> { > [...] >> if ( isset( $array[$dimension] ) ) { >> // no more dimensions to search >> if ( empty( $dimensions ) ) { >> return $array[$dimension]; >> } >> // more dimensions to search but we can only go further if >> // the current dimension value is also an array >> if ( is_array( $array[$dimension] ) ) { >> return coolFunction( $array[$dimension], $dimensions[0] ); > [...] > > That looks very promising! Great feedback! > I will try this today and feedback. The function above works pretty well for my needs, many thanks for your valuable feedback! Cheers, Nils -- http://webint.cryptonode.de / a Fractal project
Re: [PHP] Preferred Syntax
On Dec 14, 2011, at 1:53 PM, > wrote: The key thing to remember here is that this is a preference and not a performance thing. Thank you... this is basically what I wanted to know. I was concerned that not breaking the VARS out separately from the echo'ed text might cause some sort of performance problem or confusion for PHP. Therefore, whenever I came across them, I was breaking them out with the . $var . technique. Will no longer do that for existing code, as it appears not necessary, but I do prefer it for readability sake... expecially in BBEdit. So will continue to write new code breaking it out. And I too prefer a single quote for PHP and a double for HTML... even though the sample I displayed showed otherwise. Thanks to all who responded. --Rick
Re: [PHP] Unique items in an array
On 12/13/2011 1:15 PM, Marc Guay wrote: > Hi folks, > > Let's say that I have the following array: > > [0]=> > array(35) { > ["contact_id"]=> > string(3) "356" > ["contact_first_name"]=> > string(4) "Marc" > } > [1]=> > array(35) { > ["contact_id"]=> > string(3) "247" > ["contact_first_name"]=> > string(4) "Marc" > } > [2]=> > array(35) { > ["contact_id"]=> > string(3) "356" > ["contact_first_name"]=> > string(4) "Marc" > } > > And I would like to filter out exact duplicates, such as key 0 and key > 2 in this example, leaving me with an array containing only unique > entries. How would you go about it? > > Thanks for any help, > Marc > Assuming you want to make things unique based on the "contact_first_name" field, how would you decide which record to keep? The first one you run in to, the last one you come across, or some other criteria? -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Preferred Syntax
> -Original Message- > From: Adam Richardson [mailto:simples...@gmail.com] > Sent: Wednesday, December 14, 2011 2:19 PM > To: Rick Dwyer > Cc: PHP-General > Subject: Re: [PHP] Preferred Syntax > > On Wed, Dec 14, 2011 at 7:59 AM, Rick Dwyer > wrote: > > > Hello all. > > > > Can someone tell me which of the following is preferred and why? > > > > echo " > href='/mypage.php/$page_id'>$**page_name"; > > > > echo " > href='/mypage.php/".$page_id."**'>".$page_name.""; > > > > When I come across the above code in line 1, I have been changing it > to > > what you see in line 2 for no other reason than it delineates out > better in > > BBEdit. Is this just a preference choice or is one method better > than the > > other? > > > > I prefer sending arguments to the echo language construct (note, if you > send more than one argument, you can't use parentheses.) I perceive > this > usage to be a clean presentation of the code's intent, easy to use in > most > IDE's, and it's very fast relative to the other options: > > echo " href='/mypage.php/$page_id'>$**page_name"; > > echo " href='/mypage.php/".$page_id."**'>".$page_name.""; > > echo " href='/mypage.php/", $page_id, "'>", $**page_name, ""; > > And, for longer lines, I'll often break it up into separate lines by > argument like below: > > echo > " href='/mypage.php/", > $page_id, > "'>", > $**page_name, > ""; > > That all said, I don't change code that uses another convention, as I > think > it's most beneficial to stay with the established conventions in any > codebase (unless you're establishing a new convention and refactoring > the > entire code base.) This is just my general preference, and I don't > believe > there is consensus as to the most appropriate. > > Adam > > -- > Nephtali: A simple, flexible, fast, and security-focused PHP framework > http://nephtaliproject.com Adam, You are very correct, the last discussions and testing of theories caused my head to hurt!!! Neither side gained ground in testing or discussion. Having said that to me it is a preference only and not a performance enhancing or degrading factor in syntax. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
On Wed, Dec 14, 2011 at 7:59 AM, Rick Dwyer wrote: > Hello all. > > Can someone tell me which of the following is preferred and why? > > echo " href='/mypage.php/$page_id'>$**page_name"; > > echo " href='/mypage.php/".$page_id."**'>".$page_name.""; > > When I come across the above code in line 1, I have been changing it to > what you see in line 2 for no other reason than it delineates out better in > BBEdit. Is this just a preference choice or is one method better than the > other? > I prefer sending arguments to the echo language construct (note, if you send more than one argument, you can't use parentheses.) I perceive this usage to be a clean presentation of the code's intent, easy to use in most IDE's, and it's very fast relative to the other options: echo "$**page_name"; echo "".$page_name.""; echo "", $**page_name, ""; And, for longer lines, I'll often break it up into separate lines by argument like below: echo "", $**page_name, ""; That all said, I don't change code that uses another convention, as I think it's most beneficial to stay with the established conventions in any codebase (unless you're establishing a new convention and refactoring the entire code base.) This is just my general preference, and I don't believe there is consensus as to the most appropriate. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com
RE: [PHP] Preferred Syntax
> -Original Message- > From: Tamara Temple [mailto:tamouse.li...@tamaratemple.com] > Sent: Wednesday, December 14, 2011 1:40 PM > To: Tedd Sperling > Cc: Rick Dwyer; PHP-General > Subject: Re: [PHP] Preferred Syntax > > Tedd Sperling wrote: > > On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote: > > > > > Hello all. > > > > > > Can someone tell me which of the following is preferred and why? > > > > > > echo " href='/mypage.php/$page_id'>$page_name"; > > > > > > echo " href='/mypage.php/".$page_id."'>".$page_name.""; > > > > > > When I come across the above code in line 1, I have been changing > it to what you see in line 2 for no other reason than it delineates out > better in BBEdit. Is this just a preference choice or is one method > better than the other? > > > > > > --Rick > > > > Neither. > > > > My advice, take all the style elements out of the anchor tag. > > > > echo("$page_name"); > > > > Even the '' can be (perhaps should be) handled by css. > > I think you may have missed the question. The OP was asking whether: > > "text $var text" > > or > > "text ".$var." text" > > was preferrable. > > For me, I tend to use the first unless the second makes things clearer > where they need to be. Syntax hilighing can easily be one of those > times. > > > -- The key thing to remember here is that this is a preference and not a performance thing. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
On 11-12-14 01:10 PM, David Harkness wrote: On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer wrote: Can someone tell me which of the following is preferred and why? echo "$**page_name"; echo "".$page_name.""; On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford wrote: Horses for courses. I use whatever I feel like at the time, and mix various styles, as long as it's readable! I agree with Peter here. I would bet that the string with embedded variables is parsed once when the file is loaded and turned into the same bytecode as the second form. If you are going to use the second style above, I would at least switch the quotes around so you can use the double-quotes in the HTML as that to me reads nicer and avoids the minor cost of scanning the string for embedded code. I also put spaces around the dots to make the variable concatenation easier to spot when skimming it. echo '' . $page_name .''; +1 on the use of single quotes instead of double quotes. With a bytecode cache it's not really going to make a lick of difference, but using double quotes for HTML seems far more palatable to me. I too like to pull the variable out into code space. I also like to format my attributes for easy reading and commenting: echo '' .$page_name .'' .''; Although, I usually only do the above for tags that have lots of attributes. Otherwise the following is more likely: echo '' .'Something something' .''; As I said before though, a bytecode cache with any degree of optimization "should" make any particular style have zero impact on runtime (beyond original parse) by optimizing the strings into a minimal set of operations. For instance 'foo'.'fee' would be coverted to 'foofee' by the bytecode engine. Cheers, Rob. -- E-Mail Disclaimer: Information contained in this message and any attached documents is considered confidential and legally protected. This message is intended solely for the addressee(s). Disclosure, copying, and distribution are prohibited unless authorized. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
Tedd Sperling wrote: > On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote: > > > Hello all. > > > > Can someone tell me which of the following is preferred and why? > > > > echo " > href='/mypage.php/$page_id'>$page_name"; > > > > echo " > href='/mypage.php/".$page_id."'>".$page_name.""; > > > > When I come across the above code in line 1, I have been changing it to > > what you see in line 2 for no other reason than it delineates out better in > > BBEdit. Is this just a preference choice or is one method better than the > > other? > > > > --Rick > > Neither. > > My advice, take all the style elements out of the anchor tag. > > echo("$page_name"); > > Even the '' can be (perhaps should be) handled by css. I think you may have missed the question. The OP was asking whether: "text $var text" or "text ".$var." text" was preferrable. For me, I tend to use the first unless the second makes things clearer where they need to be. Syntax hilighing can easily be one of those times. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
On Wed, Dec 14, 2011 at 4:59 AM, Rick Dwyer wrote: > Can someone tell me which of the following is preferred and why? > > echo " href='/mypage.php/$page_id'>$**page_name"; > > echo " href='/mypage.php/".$page_id."**'>".$page_name.""; > On Wed, Dec 14, 2011 at 9:09 AM, Peter Ford wrote: > Horses for courses. I use whatever I feel like at the time, and mix > various styles, as long as it's readable! I agree with Peter here. I would bet that the string with embedded variables is parsed once when the file is loaded and turned into the same bytecode as the second form. If you are going to use the second style above, I would at least switch the quotes around so you can use the double-quotes in the HTML as that to me reads nicer and avoids the minor cost of scanning the string for embedded code. I also put spaces around the dots to make the variable concatenation easier to spot when skimming it. echo '' . $page_name . ''; David
Re: [PHP] Re: Preferred Syntax
On 14/12/11 16:48, Al wrote: On 12/14/2011 7:59 AM, Rick Dwyer wrote: Hello all. Can someone tell me which of the following is preferred and why? echo "$page_name"; echo "".$page_name.""; When I come across the above code in line 1, I have been changing it to what you see in line 2 for no other reason than it delineates out better in BBEdit. Is this just a preference choice or is one method better than the other? --Rick This not a PHP subject and should not be here. However, styles should be in the style block or better in the styles CSS file. Spend some time learning about CSS3 and modern techniques. With respect to tedd and Al, you've misread the question: the important PHP-related bit is about whether to embed variables in double-quoted strings or to concatenate them. These are only two of the options, and each has it's pros and cons. There has been (some time ago) plenty of discussion on this list about this sort of thing, including (ISTR) a timed test of the performance implications of various forms - that only really matters in big loops, of course... Horses for courses. I use whatever I feel like at the time, and mix various styles, as long as it's readable! Cheers Pete -- Peter Ford, Developer phone: 01580 89 fax: 01580 893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Re: Preferred Syntax
On 12/14/2011 7:59 AM, Rick Dwyer wrote: Hello all. Can someone tell me which of the following is preferred and why? echo "$page_name"; echo "".$page_name.""; When I come across the above code in line 1, I have been changing it to what you see in line 2 for no other reason than it delineates out better in BBEdit. Is this just a preference choice or is one method better than the other? --Rick This not a PHP subject and should not be here. However, styles should be in the style block or better in the styles CSS file. Spend some time learning about CSS3 and modern techniques. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
> BBEdit. Is this just a preference choice or is one method better than the > other? As far as I know it's just preference. Your choice of editor could influence your decision; one form might be given nicer highlighting. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preferred Syntax
On Dec 14, 2011, at 7:59 AM, Rick Dwyer wrote: > Hello all. > > Can someone tell me which of the following is preferred and why? > > echo " href='/mypage.php/$page_id'>$page_name"; > > echo " href='/mypage.php/".$page_id."'>".$page_name.""; > > When I come across the above code in line 1, I have been changing it to what > you see in line 2 for no other reason than it delineates out better in > BBEdit. Is this just a preference choice or is one method better than the > other? > > --Rick Neither. My advice, take all the style elements out of the anchor tag. echo("$page_name"); Even the '' can be (perhaps should be) handled by css. Cheers, tedd _ t...@sperling.com http://sperling.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Preferred Syntax
Hello all. Can someone tell me which of the following is preferred and why? echo "$page_name"; echo "".$page_name.""; When I come across the above code in line 1, I have been changing it to what you see in line 2 for no other reason than it delineates out better in BBEdit. Is this just a preference choice or is one method better than the other? --Rick -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Question about socket_select
On Wed, Dec 14, 2011 at 1:25 AM, Matijn Woudt wrote: > On Wed, Dec 14, 2011 at 12:11 AM, Mihai Anghel > wrote: >> Hello, >> >> It appears to me that something is strange with the socket_select function. >> From what I understand the value of the fourth parameter, tv_sec, >> should block the execution of the script for that number of seconds. >> I tried this code : >> > >> error_reporting(E_ERROR); >> >> $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); >> >> $result = socket_bind($serverSocket, "127.0.0.1", "20668"); >> >> $start = time(); >> >> while(true) >> { >> $reads = array($serverSocket); >> $writes = null; >> $except = null; >> $changes = socket_select($reads, $writes, $except, 5); >> $now = time(); >> echo $now - $start; >> echo "\n"; >> } >> >> and when I run it with php -q server3.php the ouput shows something >> like 0 0 0 0 0 1 1 1 1 1 2 2 2 2 etc so the script doesn't pause on >> socket_select until it returns. >> >> Cam somebody explain me what's happening ? >> > > It seems to me that your socket_select function is failing, maybe > because earlier code is failing. Check the return of socket_select > like this: > if ($changes === false) { > echo "socket_select() failed, reason: " . > socket_strerror(socket_last_error()) . "\n"; > } > > Cheers, > > Matijn Thanks for your suggestion, I reviewed the code and I saw that I was missing : socket_listen($serverSocket) . After adding this it worked like expected -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] How to use a variable variable with an array
maybe something like this if you are willing to get rid of your weird array dimension delimiters (you can use a dot for that for example, see below) $a = array( "a" => array( "b" => array( "c" => array( "d" => array( "e" => array( "f" => 1 ) ) ) ) ) ); function coolFunction( array $array, $search, $separator = '.' ) { if ( empty( $array ) ) { return false; } if ( strlen( $search ) === 0 ) { return false; } $dimensions = explode( $separator, $search, 2 ); $dimension = array_shift( $dimensions ); if ( isset( $array[$dimension] ) ) { // no more dimensions to search if ( empty( $dimensions ) ) { return $array[$dimension]; } // more dimensions to search but we can only go further if // the current dimension value is also an array if ( is_array( $array[$dimension] ) ) { return coolFunction( $array[$dimension], $dimensions[0] ); } else { return false; } } else { return false; } } var_dump( coolFunction( $a, "a.b" ) ); var_dump( coolFunction( $a, "a.b.d.e.c" ) ); var_dump( coolFunction( $a, "a.b.c.d.e.f" ) ); var_dump( coolFunction( $a, "a.b.c.d.e.f.g" ) ); On Wed, Dec 14, 2011 at 3:27 AM, Nils Leideck wrote: > Hi Al, > > many thanks for your feedback. Unfortunately I don’t know the deepness of > the array so I can’t use the nested foreach() idea :-( Let me try to > simplify my question (which also helps myself to clarify my process ;-) > > I have a variable where the value is a string that is exactly the path to > the array value: > > [code] >$arrayPath = > "['user_interface’][‘design']['my_colors']['item_number_one’]”; > [/code] > > And I have an array that has all the details: > > [code] >$myArray = coolFunction(‘getArray’); > [/code] > > The question is, how can I use these both informations to return a value > from the $myArray? > > Examples I tried unsuccessfully: > > [code] >[...] >echo $myArray$arrayPath; >echo $myArray{$arrayPath}; >echo $myArray${arrayPath}; >echo $myArray${$arrayPath}; >echo $myArray.$arrayPath; >echo $myArray.$arrayPath; >[...] > [/code] >etc... > > your feedback is much much much appreciated!!! > > Cheers, Nils > -- > http://webint.cryptonode.de / a Fractal project > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > >