Lineman Posted April 8, 2008 Share Posted April 8, 2008 Ok, here's a real n00b question for you all. I hope you don't mind! Have been practicing with variables and have come across two different ways to call them. For instance, if i was to create a variable called numbers and the give it a value of.....736 for example, see the code below: <?php $numbers = 736; print ($numbers); print "<br><br>"; print "$numbers"; ?> The two different methods above of calling the variable would both output: 736 but which is considered the best method? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/ Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 The parenthesis are usually used for multiple things like math equations print (5+6)*2; // output = 22 So you can use them or not use them depending on your preference, but if you are only outputting one thing them the less typing you have to to the better. also variables don't have to be in quotes print $numbers; Ray Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/#findComment-512176 Share on other sites More sharing options...
Lineman Posted April 8, 2008 Author Share Posted April 8, 2008 Just a matter of curiousity on my part and a desire to learn but big thanks for enlightening a n00b. Cheers Craygo Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/#findComment-512181 Share on other sites More sharing options...
wildteen88 Posted April 8, 2008 Share Posted April 8, 2008 When echo'ing/print'ing variable you do not need to use quotes or parentheses. Either of the following is fine: <?php echo $var; // no need to use quotes when echo'ing a variable on its own echo "<b>$var</b>"; // variables are parsed in strings which start with double quotes. echo '<b>'. $var . '</b>'; // variables are not parsed in strings which start with single quotes. Instead you can use concatenation. ?> NOTE: echo and print are the same. print was derived from Perl Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/#findComment-512184 Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 NOTE: echo and print are the same. print was derived from Perl Not quite - print returns a value(1), echo does not. Echo can take multiple parameters, print can not. I know im splitting hairs, but i only found this out today Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/#findComment-512187 Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 To elaborate on wildteen there is a difference between using single quotes and double quotes. Single quotes treat everything inside exactly as they are. So $name = "Ray"; echo 'My name is $name'; // will output My name is $name echo "My name is $name"; // will output My name is Ray Also double quotes allows you to use brackets for array pieces and special codes like a line break so your source code is not all on the same line $r['name'] = "Ray"; echo "My name is {$r['name']}<br>\n"; echo "Hello $name"; when you view your source code now it will look like My name is Ray<br> Hello Ray instead of My Name is Ray<br>Hello Ray Quote Link to comment https://forums.phpfreaks.com/topic/100174-which-is-best/#findComment-512194 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.