dbx Posted November 12, 2008 Share Posted November 12, 2008 Since I started coding in PHP I have been including variables in echo statements like this: echo "My name is ".$name." and I am 5 years old"; I've just realised that I can simply type: echo "My name is $name and I am 5 years old"; Can anyone tell me why I would have been doing it the first way? Is it the proper way to do it? From a previous version? Did I just pick the wrong tutorial to read? =( Thanks Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/ Share on other sites More sharing options...
Adam Posted November 12, 2008 Share Posted November 12, 2008 The second way will work, but it's good coding practise to do it the first way, or to put curly brackets around the variable: echo 'hello my name is {$name}'; Adam Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/#findComment-688366 Share on other sites More sharing options...
PFMaBiSmAd Posted November 12, 2008 Share Posted November 12, 2008 Can anyone tell me why I would have been doing it the first way?Because you like to type the extra characters and you like to solve syntax errors that are easier to make using that syntax. The second method is simpler and results in fewer typo's that produce syntax errors. However, sometimes php needs help in figuring out where variables start and stop using the second method and you will need to surround them with {}. You need to do this with array variables and with single variables that have characters following them that are valid for a variable name but are not part of that actual variable name. Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/#findComment-688368 Share on other sites More sharing options...
dbx Posted November 12, 2008 Author Share Posted November 12, 2008 In regards to coding practice, is it better to use curly brackets or the long syntax error prone way? Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/#findComment-688374 Share on other sites More sharing options...
redarrow Posted November 12, 2008 Share Posted November 12, 2008 using the {} tell php that there going to be a start and stop of a varable, so in essance it good code pratace to use {} when echo varable statements........ <?php $mes="hi there im redarrow"; echo "what your name $mess"; // this tell php echo a varable regardless if it there or not....... echo "<br>"; echo "what your name {$mess}"; // {} this tell php there a start and stop statement within a varable..... ?> Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/#findComment-688379 Share on other sites More sharing options...
dbx Posted November 12, 2008 Author Share Posted November 12, 2008 OK. thanks for your help, guys! Quote Link to comment https://forums.phpfreaks.com/topic/132406-solved-including-variables-in-an-echo-statement/#findComment-688393 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.