Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string joining in variable

Status
Not open for further replies.

steveB2006

Programmer
Feb 13, 2006
11
CA
I am having trouble adding string values to a variable,
also tried $ref .= $tmp;

$ref .= $ref.$tmp; <== crashes apache

Code:
$referer = $_SERVER['HTTP_REFERER'];

$counter = strlen($referer);
$ref = " ";
$searchChar = "/";

for($i = 7; $i < $counter; $i++)
//just get after http:// to first slash
{
	$tmp = substr ( $referer, $i , 1 );

	if ($tmp != $searchChar)//string value not slash add to string
	{
		//echo $tmp;//works fine
		$ref = $ref.$tmp;
	}
	else
		return 0; //break

}
echo $ref;//no string

I am out of ideas.
cheers
 
Your else is returning without allowing the final echo to print.

To see what's causing the problem, try this:
Code:
  else
  {
    echo $tmp . ' broken';
    return 0; //break
  }

Lee
 
The return 0 statement was breaking the entire script not just the loop.

I simply substituted break; for return 0; and it works fine.
 
I thought that using the break; statement in a loop was bad form?
 
Using return breaks you out of the next highest function block completely and terminates code execution. If you use it outside of a function, it will stop the PHP code on the page from being executed at that point. break was designed to terminate looping or other conditional statements. You could rewrite the loop statement to include the second condition for termination if you want, use a while or do loop, or just modify the second part of the for loop statement.

There are 2 ways to create conditions to terminate a loop: in the loop statement, and with breaks in the body of the loop. I'm not sure where you heard that it's bad form to break out of a loop with a break; statement, or why you thought that return; was more acceptable according.

I know in some academic settings the teachers emphasize having one exit point from blocks (functions, loops, other conditional statements) and not using break OR return inside the block, but that's to get students to think before coding and not just throw things together and then create patches with those statements for exceptions in poorly written code.

Lee
 
Some (philosofic;) addition:
Sometimes we need this pattern:
Code:
loop
   Prepare the step (more than assign one or two values).
   Ready to go? Break if not...
   Do the step.
endloop
I think in this circumstances
Code:
for (;;)
{ 
   ... 
   if (...) break;   
   ...
}
is much more cleaner (and honest;) solution than puristic (and laboured;)
Code:
$goon = 1;
while ($goon)
{
   ...
   if (...)
      $goon = 0;
   else
   {
      ...
   }
}
 
Thanks for the input. Yes Lee, your answer regarding the academic setting hit the nail on the head.

Thanks again...

steveB
 
A simple rewrite of your code to avoid the break; could be this:
Code:
$referer = $_SERVER['HTTP_REFERER'];
$ref = " ";
$searchChar = "/";
$i = 7;

while (substr ( $referer, $i , 1 ) != $searchchar)
{
  $ref .= substr ( $referer, $i , 1 );
  $i++;
}

echo $ref;

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top