galvin Posted July 6, 2011 Share Posted July 6, 2011 If you are using this operator (if it has a specific name, please let me know )... .= Is it proper for the FIRST instance of it to include the period? For example, which of these is correct... NOTE: the only difference is that the first line in one has the period, the other one does not. They both seem to work but is one proper? $I = "<td>"; if ($alldata== "") { $I .= "<img src='images/noimage.png'>"; } else { $I .= "<img src='" . $alldata['image'] . "' />"; } $I .= "<td>"; or $I .= "<td>"; if ($alldata== "") { $I .= "<img src='images/noimage.png'>"; } else { $I .= "<img src='" . $alldata['image'] . "' />"; } $I .= "<td>"; Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/ Share on other sites More sharing options...
cyberRobot Posted July 6, 2011 Share Posted July 6, 2011 Including the dot in the first line will likely throw a warning if you enable all errors & warnings. error_reporting(-1); Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1238768 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 http://us3.php.net/manual/en/language.operators.string.php Since the .= references the variable (it's the same as $var = $var . 'some new string' using your second example causes a php undefined error to be triggered on the first line using .= since the $I variable doesn't initially exist. Php must handle the error and this takes a handful of extra microseconds for the error checking and handling code to run each time and depending on your error_reporting, display_errors, and log_errors settings will cause an error message to be output to the browser or logged to the error log file (which will cause your log file to fill with errors that will make finding actual errors harder.) The first code example you posted should be used, since it initially creates the $I variable and does not produce any php errors. Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1238770 Share on other sites More sharing options...
galvin Posted July 6, 2011 Author Share Posted July 6, 2011 great, thanks so much guys! Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1238771 Share on other sites More sharing options...
PFMaBiSmAd Posted July 6, 2011 Share Posted July 6, 2011 @cyberRobot, setting the error_reporting level just stops the error from being reported. Php must still detect and handle the error every time it occurs. It's always best to write code that does not produce any php errors, warnings, or notices during its normal and expected execution. You should only get errors for unexpected things so that you can find and fix what is causing the error, such as a hacker trying to break into your script or a legitimate visitor doing something that your code did not take into account. Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1238772 Share on other sites More sharing options...
cyberRobot Posted July 6, 2011 Share Posted July 6, 2011 @cyberRobot, setting the error_reporting level just stops the error from being reported. Php must still detect and handle the error every time it occurs. It's always best to write code that does not produce any php errors, warnings, or notices during its normal and expected execution. You should only get errors for unexpected things so that you can find and fix what is causing the error, such as a hacker trying to break into your script or a legitimate visitor doing something that your code did not take into account. Yep, but it can also show when something isn't technically correct. Without error_reporting(); being set to something like -1, the OP's code appears to work just fine. Once the error reporting code is added, the page will show a notice for an undefined variable. Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1239007 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 You guys agree, haha Just make sure you use both error_reporting(-1); and ini_set("display_errors", 1); Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1239009 Share on other sites More sharing options...
cyberRobot Posted July 6, 2011 Share Posted July 6, 2011 Just make sure you use both error_reporting(-1); and ini_set("display_errors", 1); The notice displays for me without using ini_set(), but I suppose it depends on how the server/page is set up. Thanks for pointing that out. Of course, you'll want to use both if you're looking to hide errors on a "live" website. Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1239020 Share on other sites More sharing options...
xyph Posted July 6, 2011 Share Posted July 6, 2011 Everything displays for me by default. It's set up that way in my php.ini. This isn't true for everyone, and generally only telling someone to set error_reporting() isn't enough. If error_reporting is turned off, display_errors could easily be off as well. Quote Link to comment https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/#findComment-1239026 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.