Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
I am starting to learn to use single quotes for strings. I agree in that it keeps code cleaner, variables out of strings, etc. The tabs issue is a new one... I've always done a tab. It's easier and faster than hitting <space> 4 times.
also the fact theres no PHPc
meaning no client-side version of PHP, i know it will never happen but you could do some truly amazing things if there was...........
IMHO, single over double quotes is a question of consistency - choose a way and stick with it (personally I like my variables out of strings so I can see them). You shouldn't really ever need to use things like \n in quoted strings, heredoc seems much more suited to that type of thing.
dunno why i hate the first example, its just lots of completely unnecessary code
I'll tell you why I would use a variation of the first example.. because it would be easier to expand. In other words, imagine you wanted to output a pretty html page for the database connection failing.
if(!$link)
{
ob_start;
$smarty = new smarty;
$smarty->output('db-error.tpl');
ob_end;
//die("could not connect to MySQL");
}
Obviously non-functional code, but you get the idea.. multiple commands can be added without any muss/fuss. In the second example, you have to rewrite the "either/or" code - possibly changing it to the above form.
I always use full bracketed logic sections for *all* decision branches - ensuring the code can easily be changed in the future and extended in new directions with a minimum of changes hitting CVS. (I'm a huge CVS fan, and want each change to be unique, specific, and obvious whenever possible).
dunno why i hate the first example, its just lots of completely unnecessary code
I'll tell you why I would use a variation of the first example.. because it would be easier to expand. In other words, imagine you wanted to output a pretty html page for the database connection failing.
<?php
include ("cfg/error_cfg.php");
// defined in error config would be 2 variables
// $connect_error = "<html>...<body><h1>some text</body></html>";
// $database_error = "<html>...<body><h1>some text</body></html>";
$user = "";
$pass = "";
$server = "";
$db = "";
mysql_connect ($server, $user, $pass) or die($connect_error);
mysql_select_db($db) or die($database_error);
?>
still gives you customisable error messages (and pages deciding on whether you decide to create the variables with tags in them) without the unnecessary amount of code,
<?php
include ("cfg/error_cfg.php");
// defined in error config would be 2 variables
// $connect_error = "<html>...<body><h1>some text</body></html>";
// $database_error = "<html>...<body><h1>some text</body></html>";
$user = "";
$pass = "";
$server = "";
$db = "";
mysql_connect ($server, $user, $pass) or die($connect_error);
mysql_select_db($db) or die($database_error);
?>
still gives you customisable error messages (and pages deciding on whether you decide to create the variables with tags in them) without the unnecessary amount of code,
Just my personal preference anyway....
But then you are just avoiding the issue - which is that you cant expand the functionality of the "if/then/else/or/when" logic branches.
What if you wanted to for example, write to an error log file, then display the page, and then do a redirect to a report-bug-to-admin page? Thats three actions - you'd need brackets again, and would have to rewrite the code.
Thats why I always fully expand logic branches. Its longer, absolutely. But it gives fantastic flexibility, and - FOR ME - its easier to read and troubleshoot.
malcolmboston wrote:lol, well thats why your better than me
Not at all.. there are many that could (quite correctly) argue that my code is not 'easy to read', not 'tight', and way too over-generalized (see refactoring for purpose).
Everyone has their own "best" way of doing things. Some actually *are* truly better across the board, but that is fairly rare.
I've said it before, and I'll say it again - if there was only one RIGHT way to do things, php would be Python.
That's how I do it, it looks cleaner to be and less jumbled up to me with the spacing, but I don't think either way is bad practice, just a matter of preference
for($i=100;$i>0;$i--){
if($i != 1){
echo $i . " bottles of beer on the wall, " . $i . " bottles of beer. <br />";
echo "knock one down, spin it around, " .($i-1). " bottles of beer on the wall!<br />";
}
else {
echo $i . " bottle of beer on the wall, " . $i . " bottle of beer. <br />";
echo "knock it down, spin it around, no more bottles of beer on the wall!";
}
}