Re: [PHP] Re: Preferred Syntax
On 11-12-17 09:42 AM, Eric Butera wrote: Hi Adam, Thanks for the reply, noted! I was coming from the angle that I've had to deal with a lot of code that is 2000 lines of php/html/javascript inside heredocs, mixed quote escaping, etc. I was hoping to prevent that from becoming a new thing in this persons code if that was the case. Apologies for assuming. I understand that point of view, but your overarching statement of using full separation of presentation and logic at all times is wrong in the general case. Sometimes a 10 line script of mixed logic and presentation is just what the doctor ordered... but that might happen more often when doing small concise command-line scripts :) 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] Re: Preferred Syntax
On Sat, Dec 17, 2011 at 12:59 AM, Adam Richardson simples...@gmail.com wrote: On Fri, Dec 16, 2011 at 11:53 PM, Eric Butera eric.but...@gmail.com wrote: To all the people who responded to this thread: It is 2011 - please stop writing code like this. To the OP: I'm glad you're asking questions and realizing you're not happy with your current abilities and suspect there's a better way. I've read the replies in this thread and feel a bit let down. Use a templating language - yes, I understand that is what php is for - but I won't go into it. You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. Please separate your concerns. Not sure what that means? That's OK! If you want to move forward, look up how modern frameworks deal with this issue using their views or template views. You don't have to use a framework if you do not want to, that's perfectly fine. If it works, it works. But in the end, it the separation of logic and html is essential to code maintenance. Eric, There are many posters to this list, and there exists a broad range of programming styles and abilities. I'll bet you're a competent programmer, and that you've worked hard to hone your craft. It takes passion and drive to improve one's skill set. However, I'd encourage you to focus that passion on the list in a way that facilitates the growth of those with questions whilst staying true to their current, specific needs. Frankly, every answer on the list could begin with the suggestion that they just use a framework. The list is here to help build up the entire skill set of PHP developers. Let's reexamine the original post: Hello all. Can someone tell me which of the following is preferred and why? Use of the word Which implies that there were a closed set of options they wanted to consider, although we did offer some others, but they all stayed relatively true to his original options. echo a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/.$page_id.'.$page_name./abr; Simple. Please note there is no logic anywhere in this example. PHP is truly serving merely as a templating language here. So, while I agree with the general notion that logic should not be intermingled with markup, this particular example does not serve as the anti-pattern you suggest. Also, note that we aren't sure where the $page_id and $page_name variables are coming from. In instances where these are set manually within the script (like a view variables at the top of the page), there's no need to escape anything. That said, you're right, if the data is coming from somewhere else, escaping should happen, but there's not enough information to infer that, as you say, You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. 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? The above statement suggests there's an existing codebase that was being worked through. In this light, the answers mostly focused on answering the OP's original question, realizing that this was existing code that he was refactoring lightly as he goes. This is not to say that I disagree with all that you said, as I actually developed my own framework that: Cleanly separates PHP from HTML to avoid the intermingling of logic and presenation: http://nephtaliproject.com/documentation/markup/ Automatically handles output escaping, input validation: http://nephtaliproject.com/documentation/examples/contact.php And lots of other features that coincide with the general focus of your words. Given that work, I think it's fair to say that I do agree with several of your general points for web development overall. However, this question wasn't a big picture question on how to do web development with PHP. It was a simple question that was answered in a helpful, specific manner by several on the list. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com Hi Adam, Thanks for the reply, noted! I was coming from the angle that I've had to deal with a lot of code that is 2000 lines of php/html/javascript inside heredocs, mixed quote escaping, etc. I was hoping to prevent that from becoming a new thing in this persons code if that was the case. Apologies for assuming. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Preferred Syntax
On Fri, 16 Dec 2011 23:53:46 -0500, Eric Butera wrote: To all the people who responded to this thread: It is 2011 - please stop writing code like this. To the OP: I'm glad you're asking questions and realizing you're not happy with your current abilities and suspect there's a better way. I've read the replies in this thread and feel a bit let down. Use a templating language - yes, I understand that is what php is for - but I won't go into it. You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. Please separate your concerns. By and large, I agree with you, and certainly it is a good idea to raise that with the OP since they were asking about recommended practices. However, should not is not the same as don't and there are still places where this is appropriate. Here's a chunk of real code from a WordPress plugin, taken from the class that extends the Page admin form to provide a button for selecting a custom associated image: echo HTML input type=button class=button mmm-admin-image-button data-fieldname=$fieldName value=Select id=mmm-admin-image-button-$fieldName / input type=hidden name=$fieldName value=$fieldValue / span id=mmm-admin-image-caption-$fieldName {$this-getAttachmentCaption($fieldValue)}/span input type=button class=button mmm-admin-clear-button data-fieldname=$fieldName value=Clear / HTML; HEREDOC provides me a way to do templating in a simple way within a WordPress hook function. The purpose of the hook is to provide a way to augment WordPress Page editing by adding some HTML; sending a small fragment of HTML to the browser is its function. I gain nothing by spinning that fragment out to a template file somewhere. For other hook functions (e.g. representing shortcodes), it is beneficial to use templating, and I do so -- but horses for courses. Not sure what that means? That's OK! If you want to move forward, look up how modern frameworks deal with this issue using their views or template views. You don't have to use a framework if you do not want to, that's perfectly fine. If it works, it works. But in the end, it the separation of logic and html is essential to code maintenance. Applause! :) -- Ross McKay, Toronto, NSW Australia The chief cause of problems is solutions -Eric Sevareid -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Preferred Syntax
On Thu, Dec 15, 2011 at 5:46 PM, Ross McKay ro...@zeta.org.au wrote: Jim Lucas wrote: I second this example, with one minor change, I would add '{' and '}' around variables. echo HTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/{$page_id}{$page_name}/abr HTML; This works for $variables, $objects, and variable functions calls. But doesn't work if you try to call functions directly (bummer). In fact, we are in agreement here :) I was just simplifying for the example at hand. And as for calling functions directly, also add constants :( However, it's easy enough to assign a constant to a variable and embed it in a HEREDOC, and also easy to wrap a function in a method, especially when your HEREDOC is within a method itself: define('MSG', 'My name is'); class X { function html($text) { return htmlspecialchars($text); } function output($name) { $msg = MSG; echo HTML p$msg {$this-html($name)}/p HTML; } } $x = new X(); $x-output('silly rockstar name like '); [...] This is why I like heredoc syntax over pretty much everything else. Concur! -- Ross McKay, Toronto, NSW Australia Pay no attention to that man behind the curtain - Wizard of Oz -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php To all the people who responded to this thread: It is 2011 - please stop writing code like this. To the OP: I'm glad you're asking questions and realizing you're not happy with your current abilities and suspect there's a better way. I've read the replies in this thread and feel a bit let down. Use a templating language - yes, I understand that is what php is for - but I won't go into it. You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. Please separate your concerns. Not sure what that means? That's OK! If you want to move forward, look up how modern frameworks deal with this issue using their views or template views. You don't have to use a framework if you do not want to, that's perfectly fine. If it works, it works. But in the end, it the separation of logic and html is essential to code maintenance. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Preferred Syntax
On Fri, Dec 16, 2011 at 11:53 PM, Eric Butera eric.but...@gmail.com wrote: To all the people who responded to this thread: It is 2011 - please stop writing code like this. To the OP: I'm glad you're asking questions and realizing you're not happy with your current abilities and suspect there's a better way. I've read the replies in this thread and feel a bit let down. Use a templating language - yes, I understand that is what php is for - but I won't go into it. You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. Please separate your concerns. Not sure what that means? That's OK! If you want to move forward, look up how modern frameworks deal with this issue using their views or template views. You don't have to use a framework if you do not want to, that's perfectly fine. If it works, it works. But in the end, it the separation of logic and html is essential to code maintenance. Eric, There are many posters to this list, and there exists a broad range of programming styles and abilities. I'll bet you're a competent programmer, and that you've worked hard to hone your craft. It takes passion and drive to improve one's skill set. However, I'd encourage you to focus that passion on the list in a way that facilitates the growth of those with questions whilst staying true to their current, specific needs. Frankly, every answer on the list could begin with the suggestion that they just use a framework. The list is here to help build up the entire skill set of PHP developers. Let's reexamine the original post: Hello all. Can someone tell me which of the following is preferred and why? Use of the word Which implies that there were a closed set of options they wanted to consider, although we did offer some others, but they all stayed relatively true to his original options. echo a style='text-align:left;size:**14;font-weight:bold' href='/mypage.php/$page_id'$**page_name/abr; echo a style='text-align:left;size:**14;font-weight:bold' href='/mypage.php/.$page_id.**'.$page_name./abr; Simple. Please note there is no logic anywhere in this example. PHP is truly serving merely as a templating language here. So, while I agree with the general notion that logic should not be intermingled with markup, this particular example does not serve as the anti-pattern you suggest. Also, note that we aren't sure where the $page_id and $page_name variables are coming from. In instances where these are set manually within the script (like a view variables at the top of the page), there's no need to escape anything. That said, you're right, if the data is coming from somewhere else, escaping should happen, but there's not enough information to infer that, as you say, You should not be echoing, printing, or any other method of concatenating html dealing with escaping quotes inside your php logic code. 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? The above statement suggests there's an existing codebase that was being worked through. In this light, the answers mostly focused on answering the OP's original question, realizing that this was existing code that he was refactoring lightly as he goes. This is not to say that I disagree with all that you said, as I actually developed my own framework that: - Cleanly separates PHP from HTML to avoid the intermingling of logic and presenation: http://nephtaliproject.com/documentation/markup/ - Automatically handles output escaping, input validation: http://nephtaliproject.com/documentation/examples/contact.php - And lots of other features that coincide with the general focus of your words. Given that work, I think it's fair to say that I do agree with several of your general points for web development overall. However, this question wasn't a big picture question on how to do web development with PHP. It was a simple question that was answered in a helpful, specific manner by several on the list. Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com
Re: [PHP] Re: Preferred Syntax
On 11-12-15 02:50 AM, Ross McKay wrote: 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 a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/.$page_id.'.$page_name./abr; [...] Just to throw in yet another possibility: echoHTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/$page_id$page_name/abr 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. Heredoc and Nowdoc are nice but I hate the way they muck up my indentation aesthetics. As such when I use them I use as minimalist a terminator as possible: ?php echo _ a href=foo.htmlBlah blah blah/a _; ? 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] Re: Preferred Syntax
Another nice way would be sprintf. So your string really is just a string and nothing more. I don't know how it would affect performance, but just for the eye I find it much simpler. echo sprintf(a style='text-align:left;size:**14;font-weight:bold' href='/mypage.php/%d'%s/abr, $page_id, $page_name); 2011/12/15 Robert Cummings rob...@interjinn.com On 11-12-15 02:50 AM, Ross McKay wrote: 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 a style='text-align:left;size:**14;font-weight:bold' href='/ mypage.php/$page_id'$page_**name/abr; echo a style='text-align:left;size:**14;font-weight:bold' href='/ mypage.php/.$page_id.'.$**page_name./abr; [...] Just to throw in yet another possibility: echoHTML a style=text-align:left;size:**14;font-weight:bold href=/mypage.php/$page_id$**page_name/abr 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. Heredoc and Nowdoc are nice but I hate the way they muck up my indentation aesthetics. As such when I use them I use as minimalist a terminator as possible: ?php echo _ a href=foo.htmlBlah blah blah/a _; ? 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] Re: Preferred Syntax
On 12/14/2011 11:50 PM, Ross McKay wrote: 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 a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/.$page_id.'.$page_name./abr; [...] Just to throw in yet another possibility: echo HTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/$page_id$page_name/abr 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. I second this example, with one minor change, I would add '{' and '}' around variables. echo HTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/{$page_id}{$page_name}/abr HTML; This works for $variables, $objects, and variable functions calls. But doesn't work if you try to call functions directly (bummer). $ cat variable_usage.php ?php $v1 = 'Variable 1'; $o1 = new stdClass(); $o1-v1 = 'Object 1'; function something($str) { return ...{$str}...; } $f1 = something; echo _ {$v1} {$o1-v1} {$f1('Function 1')} {something('F1')} _; ? Results in this $ php -f variable_usage.php Variable 1 Object 1 ...Function 1... {something('F1')} This is why I like heredoc syntax over pretty much everything else. -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ C - (541) 408-5189 O - (541) 323-9113 H - (541) 323-4219 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: Preferred Syntax
Jim Lucas wrote: I second this example, with one minor change, I would add '{' and '}' around variables. echo HTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/{$page_id}{$page_name}/abr HTML; This works for $variables, $objects, and variable functions calls. But doesn't work if you try to call functions directly (bummer). In fact, we are in agreement here :) I was just simplifying for the example at hand. And as for calling functions directly, also add constants :( However, it's easy enough to assign a constant to a variable and embed it in a HEREDOC, and also easy to wrap a function in a method, especially when your HEREDOC is within a method itself: define('MSG', 'My name is'); class X { function html($text) { return htmlspecialchars($text); } function output($name) { $msg = MSG; echo HTML p$msg {$this-html($name)}/p HTML; } } $x = new X(); $x-output('silly rockstar name like '); [...] This is why I like heredoc syntax over pretty much everything else. Concur! -- Ross McKay, Toronto, NSW Australia Pay no attention to that man behind the curtain - Wizard of Oz -- 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 a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/.$page_id.'.$page_name./abr; 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] 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 a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/mypage.php/.$page_id.'.$page_name./abr; 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 Wed, 14 Dec 2011 07:59:46 -0500, Rick Dwyer wrote: Can someone tell me which of the following is preferred and why? echo a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/$page_id'$page_name/abr; echo a style='text-align:left;size:14;font-weight:bold' href='/ mypage.php/.$page_id.'.$page_name./abr; [...] Just to throw in yet another possibility: echo HTML a style=text-align:left;size:14;font-weight:bold href=/mypage.php/$page_id$page_name/abr 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