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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FOR loop failure 2

Status
Not open for further replies.

770

Programmer
May 21, 2003
2
0
0
US
The 1st line inside the function below gives me
this error: "... Undefined variable: search_str ..."

Yet when I substitute the numeral 3 for expr2 - it works!
--------------------------------------------------

function find_more_hits(){
for ($i = 1 ; $i < strlen($search_str) ; $i++) {
echo $search_str;
}
}

$search_str = &quot;abc&quot;;
-------------------------------

Even with everything inside the for loop commented out, I get the same error.
Can you please tell me why expression2 is not working.
Thanks - Mayer
 
You need to consider variable scope in PHP.
The variable $search_str is a local variable to the function find_more_hits. You have either to pass it through the function call or make it a global variable.
Code:
function find_more_hits($myString){...
... or ...
global $search_str;

 
You can't pass the parameter $search_str directly unless you declare it a global. Specify another var in your function, and call it with another var. Like this:


<?php

function find_more_hits($s)
for ($i=1; $i >< strlen($s); $i++) {
echo $s;
}
}
?>

// html tags for beginning of page

<body>

<?php
$search_str =&quot;abcd&quot;; find_more_hits($search_str);
?>
</body>


[thumbsup]
 
yup as DRJ478 said,

function find_more_hits($search_str){
for ($i = 1 ; $i < strlen($search_str) ; $i++) {
echo $search_str;
}
}

$search_str = &quot;abc&quot;;


Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top