Jump to content

Recommended Posts

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>";

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/241169-using-the-period-equals-operator/
Share on other sites

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.

@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.

@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.

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. :D Of course, you'll want to use both if you're looking to hide errors on a "live" website.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.