What is $Body = ""; for?

I often see something like this:

// prepare email body text
$Body = “”;
$Body .= "Name: ";
$Body .= $Name;
$Body .= "
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "
";
.
.
.


What is the

$Body = “”;

for?

I can leave it out and I see no difference.

Thank you for your help.

Jim Adrian

Hi Jim,

Try this and it may be a little more clearer.

http://johns-jokes.com/downloads/sp/undefined-variable/index.php

Top left is a download for the PHP source files.

.

Hi Jim,

Here is the output from the following code:

Start TEST
error reporting OFF
error reporting ON

Notice: Undefined variable: Name in C:\awww\index-post_error.php on line 26

Notice: Undefined variable: Email in C:\awww\index-post_error.php on line 29

// the source file


<?php
echo "<h1>Start TEST</h1>";
			// switch all errors off
			error_reporting(0);
			ini_set('display_errors', 'off');
			echo "<h1>error reporting OFF </h1>";
			
			// prepare email body text
			// $Body = "";
			$Body .= "Name: ";
			$Body .= $Name;
			$Body .= "\
";
			$Body .= "Email: ";
			$Body .= $Email;
			$Body .= "\
";
			
			// and then try this:
			// switch all errors off
			error_reporting(E_ALL);
			ini_set('display_errors', 'on');
			echo '<h1>error reporting ON</h1>';
			
			// prepare email body text
			$Body = "";
			$Body .= "Name: ";
			$Body .= $Name;
			$Body .= "\
";
			$Body .= "Email: ";
			$Body .= $Email;
			$Body .= "\
"; 
			
			echo '<fieldset style="width:55&#37;;margin:1em auto">';
			echo '<legend>Source code</legend>';
			
				highlight_file(__FILE__);		
			
			echo '</fieldset>';

It works on my computer as expected. Can you try it in a file by itself.

// What variable?
The variable I meant was $Body

Cheers,

John_Betong

.

John

Thank you for your two posts. I tried what you suggested.

I don’t get an error message whether I include ini_set(‘display_errors’, ‘on’); or not and I don’t get any error messages whether I include $Body = ‘’; or not , nor in any combination.

What variable?

Thank you for your help.

Jim Adrian

In the file at http://www.globalfreeenterprise.com/contact.php the variable $First is mentioned in the local variables section, then in the validation section, and then in the prepare body section. It is also referenced in the html form that follows the php area.

Do you mean that by including $body =“”; at the beginning of the prepare body section I prevent me from defining Body before the prepare body section? What are you referring to when you say “URL” (" . . . putting it in as a variable on the URL").

I know this must seem terribly dense on my part, but I don’t know the order in which things happen when the server executes the php and I don’t really know what each statement does in the three sections that I have mentioned. I do have a clear idea of what the html form is doing.

Thank you for your help.

Jim Adrian.

John_Betong,

I have the two files at

http://www.globalfreeenterprise.com/test1.php

and

http://www.globalfreeenterprise.com/test2.php

One contains

error_reporting(0);
ini_set(‘display_errors’, ‘off’);
echo “<h1>error reporting OFF </h1>”;

// prepare email body text
//$Body = “”;

and the other contains

error_reporting(E_ALL);
ini_set(‘display_errors’, ‘on’);
echo ‘<h1>error reporting ON</h1>’;

// prepare email body text
$Body = “”;

I just added test3.php and test4.php to try the other two possibilities.

What would you want me to notice?

Thank you for your help.

Jim Adrian

Did you try running the script I sent?

If you did then you would find that it is necessary to declare the variable before usage otherwise PHP will produce warnnings that the variable is not in use.

The first part of the script shows how to suppress undecalared variable warnings, the latter part of the script with the warnings showing.

$Body = ‘’;

By deleting the above line, then if the variable has not been previously declared a warning will be given. If $Body has been previously declared then it will retain the previous values and the remaining script will append additional values.

.

Thank you all. I get it (Finally).

Jim Adrian

To be straight to the point, the following line just initializes your $Body variable with empty value:


$Body = "";

So whatever you assign with the $Body variable before this line will be deleted/removed and variable will be made empty. So whatever you want to put with this variable you must do after this line.

Try this:



error_reporting(0);
ini_set('display_errors', 'off');

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\
";

// and then try this:
error_reporting(E_ALL);
ini_set('display_errors', 'on');

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\
";


.

All of the lines except that one are using .= to concatenate onto the end of whatever $Body already contains. Setting it to blank first allows you to easily ament the code later to add extra fields at the start of the $Body.

If you just started it with $Body = "Name: "; then you’d need to also modify that line if you need to add something in front (to add back in the dot that changes it from assigning to the field to concatenate to it instead).

I Believe it’s a holdover from when magic-variables were being used…

Setting $body =“”; prevents someone from pre-loading Body with some text by putting it in as a variable on the URL. You could achieve the same effect by removing the . from in front of the top line of the actual body text.

I’m sorry to say that I don’t understand at all. Is it a typing issue? is it a cut-and-paste issue?

If I want to add

$Body .= "PinNumber: ";
$Body .= $PinNumber;
$Body .= "
";

to the top, what is the advantage of having

$Body = “”;

already there?

If I want to add

If I want to add

$Body .= "PinNumber: ";
$Body .= $PinNumber;
$Body .= "
";

To some place in the middle of the list, how does the $Body = “”; help me?

That section is labeled

// prepare email body text

That indicates to me that this section is determining what the sent email looks like as to its content and the order in which it is presented to the recipient. Is that right?

I don’t have any idea what each of these three lines

$Body .= "PinNumber: ";
$Body .= $PinNumber;
$Body .= "
";

are doing individually in particular. Maybe that is the problem, but I surely don’t know.

Thank you for your help.

Jim Adrian