Is it okay to write code like this, with statements spanning across multiple lines?
$db_selected =
mysql_select_db(
'my_dbase',
mysql_connect(
'localhost',
'mysql_user',
'mysql_password'
)
);
In HTML, new lines are ignored, but in PHP I sometimes get errors. I'm not too familiar with PHP, but thought this should be just fine, no?
Yes, but for certain things, such as text I do something like ...
$someText = " blah blah blah ".
"some more blah blah blah";
Hope this helps
UPDATE 2022 haha
I know this is super old, but another way would be using EOF
$str = <<<EOF
You can put whatever <p>text</p>. <b>you want to put</b>
in here, for as
many lines as you want ()I)(#*$)#*($)#
EOF;
No, but not for why you think. The whitespace is fine, but there is an issue with that code:
mysql_select_db(
'my_dbase',
// don't call mysql_connect here!!!
mysql_connect(
'localhost',
'mysql_user',
'mysql_password'
)
);
MySQL connect will return FALSE on error. This means that you'll not be able to handle the mysql_error()
there AND it will cause an error in mysql_select_db
.
You're better off:
$conn = mysql_connect(
'localhost',
'mysql_user',
'mysql_password'
) or die( mysql_error() );
mysql_select_db(
'my_dbase',
$conn // optional parameter, but useful nonetheless.
);
Whitespace is generally ignored, so you can insert line breaks as needed. However, you need to end each statement with a semicolon (;
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With