Jump to content

[SOLVED] Global function, echoing lots of Variables?


Recommended Posts

I have a script with lots of this in:

 

	$report.="Driver: $username<BR>";
$report.="Racing Style $racingstyle<BR>";
$report.="Car: $row[name] ($row[manufacture])<BR>";
$report.="Max Speed $maxspeed<BR>";
$report.="Acceleration (0-100) $accel<BR>";
$report.="Handling (0-10) $handling<BR>";
$report.="<BR><BR>";

 

In several functions.

 

I've seen towards the bottom of the script there is a

global $report;

 

All I want to know is how can I echo all this data, has 'global' got anything to do with this? And why isn't the variable being over written every time it is done again, e.g. why isn't the above not just "<BR><BR>"? Is it todo with the fullstop at the end of the variable?

 

Thanks,

Nick.

It's actually a full stop before the = sign, and it's a shortcut operator for string concatenation

 

"global" allows a variable defined in global scope to be accessed from within the local scope of a function

In PHP, to concatenate strings, you use the '.' operator.  '.=' is a shortcut operator that stands for this:

<?php
$string = $string . ' lolz';
// is the same as
$string .= 'lolz';
?>

 

If a variable is declared as global, that means every part of the script has access to it.

 

If you want to view the $report variable, just place

echo $report;

 

somewhere near the bottom of the script.

Beaten to it :)

.= is the concatenation assignment operator

 

Examples

 

// assignment operator
$var1 = 'one';
$var1 = 'two';

echo $var1 // ouputs: two

// concatenation assignment operator
$var1 .= 'one';
$var1 .= 'two';

echo $var1 // ouputs: onetwo

 

Thanks a lot! I can see this coming in useful.

 

However I can't echo the data, I'm trying to insert it into a database field (text) so I can read the results, it just comes out blank.

 

I have the query at the bottom of the file after the functions.

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.