PHP: var_dump while loop - php

I was trying to create a loop for all the variables on a page to display in one while loop. I couldn't seem to get the variable to output. Here is the code I tried that made the most sense to me:
<?php
$varone = true;
$vartwo = 47;
$varthree = "A little string";
$vars = get_define_vars();
while($loop = $vars){
echo "variable ".$vars;
var_dump($vars;)
}
?>
It would crash one browser and the other it would stay blank. The reason I tried the code this way is because I thought of how I would word a mysql loop and I thought it would work in a similar way. Sorry if this is a newb question.

You should use something like a foreach() loop...
foreach ($vars as $name => $var){
echo "variable ".$name."=";
var_dump($var);
}

Alternatively, I think it might be easier to use an array for this.
$new_array[] = true;
$new_array[] = 47;
$new_array[] = "A little string";
$a=0;
while($new_array[$a]){
echo "variable ".$a." = ";
var_dump($new_array[$a]);
$a++;
}

The reason I tried the code this way is because I thought of how I
would word a mysql loop and I thought it would work in a similar way.
Though I would go with foreach you can use your current code with list and each as many use to fetch from MySQL:
while(list($key, $val) = each($vars)){
echo "variable $key";
var_dump($val); //var_dump($vars;) PARSE ERROR check ;
}
However each is deprecated:
Warning This function has been DEPRECATED as of PHP 7.2.0. Relying on
this function is highly discouraged.

Related

How do I create variables based on an array? PHP

I'm quite confused in how would I be able to create variable based on an array's values, my code is:
$a = array("red","black","white","green","blue");
for($i=0;$i>5;$i++)
{
$$a[$i] = '0.00';
}
echo $red;
I was under the impression that emulating a statement that says $red = '0.00'; would run properly but alas, it says undefined variable red.
use this:
for($i=0;$i<5;$i++)
you got error in loop, you have used '>' sign, so loop doesn't work, actually... :)
It's only your assignation that is wrong.
Use a foreach loop to make it easier, and it will work :
$a = array("red","black","white","green","blue");
foreach ($a as $val) {
$$val = '0.00';
}
echo $red;
Output :
0.00

PHP Cannot display content variable inside foreach loop when combine two different variable array

I have an multiple variable like this and i want to combine two variable in foreach loop:
$foo = array(4, 9, 2);
$variables_4 = array("c");
$variables_9 = array("b");
$variables_2 = array("a");
foreach($foo as $a=>$b) {
foreach($variables_{$b} as $k=>$v) {
echo $v;
}
}
After i run above code it display error "Message: Undefined variable: variables_"
Is anyone know how to solve this problem?
You can use Variable variables to get the job done, but in this case it is kind of ugly.
A cleaner way to do this is by using nested arrays:
$foo = array(4=>array("c"),
9=>array("b"),
2=>array("a"));
foreach($foo as $a=>$b) {
foreach($b as $k=>$v) {
echo $v;
}
}
Then you won't have to create a lot of variables like $variables_9.
You should try to use eval(), for example:
foreach(eval('$variable_'.$b) as $k=>$v)...
I would highly suggest another route (this is a poor structure). But anyways...
Try concatenating into a string and then use that
$var = 'variables_' . $b;
foreach($$var as $k=>$v) {
echo $v;
}
This is a syntax error.
You need to concatenate the strings within the brackets:
${'variables'.$b}
look at this post for more info.

Passing variable variables into if statement - php

I have a series of variables in php:
move1A = $row['column'];
move2A = $row['column'];
move3A = $row['column'];
etc.
I want to create a variable that will represent these variables and check if it is NULL. I have spent quite a bit of time looking, and although I believe that variable variables may be the key to my problem. So far I have tried:
$current_move_check = 'move'.$moveCounter.'A';
and
$current_move_check = '$move'.$moveCounter.'A';
Neither seem to work. Any suggestions for making something like this work? Thanks!
UPDATE:
So I'm trying to loop the moveXA variables based on user input and I need to run different script whether it is null or set.
I thought that:
elseif (!$$current_move_check) {
and
elseif ($$current_move_check) {
would work but they seem to not be outputting as expected.
Considering your update, I'd really suggest you to use an array, rather than the variable variables trick. Your code would makes more sense and be easier to maintain :
$count = 0;
$moveA[++$count] = $row['column'];
$moveA[++$count] = $row[...];
$moveA[++$count] = $row[...];
...
foreach ($moveA as $key => $value) {
if ($value) { // = $$current_move_check
} else { // = !$$current_move_check
}
}
As #MatsLindh pointed out in its comment : "Variable variables are never a good idea. Unless you know when it makes sense to break that rule, don't."
$current_move_check = 'move'.$moveCounter.'A';
echo $$current_move_check;
now you can check this as well like
if($$current_move_check!=NULL)
{
// do your code
}

Breaking a foreach loop

I was unable to break out of a foreach loop. I think the structure is correct there is something else wrong with the code. Please let me know what is the issue,(not just a working code) i want to learn from my mistakes. Thanks
I am using simple html dom for scraping some piece of information and i want the loop to break when a condition is matched. Here is my code :
<?php
$mainjob = file_get_html('link to scrap here');
$newarr = array();
foreach($mainjob->find('td[valign=middle]') as $d) {
$data = $d->innertext;
$newarr[] = $data;
echo $data . "<br>";
if($data == "Job Opportunity Description:") {
break;
}
}
print_r($newarr);
1) I guess your $data=="Job Opportunity Description:" condition wasn't true, so it didn't call the break;
2) Put your $mainjob->find('td[valign=middle]') out of your loop conditions, so it doesn't get called each iteration:
$tds = $mainjob->find('td[valign=middle]');
foreach($td as $d){}
3) Why did you add brackets to your $newarr variable?
4) Maybe your $data string still contains the html tags (which aren't shown by echo due to your browser.. so the condition would return false.
The problem is in the white space i used trim() to trim out the white space and it works. I recommend the answer from Ayman Safadi. Thanks.

PHP error: method name must be a string

I have found a bug report(http://bugs.php.net/bug.php?id=24286) about this though I am not sure if it's relevant to my problem and it's very old. This is my code:
class RegExp {
function name($e){ .... }
function email($e){ .... }
function phone($e){ .... }
}
$regexp = new RegExp();
$final_keys= array("name", "email", "phone");
for($index=0; $index < count($final_keys); $index ++){
if(!$regexp->$final_keys[$index]($_POST[$final_keys[$index]])){
$invalid_uri_vars .= $final_keys[$index].",";
}
else{
$uri_vars = "&".$final_keys[$index]."=".$_POST[$final_keys[$index]];
}
}
What it does is it uses a value from an array as the name of the method to be called. In other words, I am trying to implement a function call using a variable $final_keys[$index] as its name.
*UPDATE: I tried implementing the suggestions below, nothing seems to work. Here's one of my modifications as suggested:
for($key=0; $key < count($final_keys); $key ++){
if(!$regexp->{$final_keys[$key]}($_POST[$final_keys[$key]])){
$invalid_uri_vars .= $final_keys[$key].",";
}
else{
$uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
}
}
I get the same error as my original code. Another method using call_user_func but I am not sure if did it right:
for($key=0; $key < count($final_keys); $key++){
if(!call_user_func(array($regexp, $final_keys[$key]), $_POST[$final_keys[$key]])){
$invalid_uri_vars .= $final_keys[$key].",";
}
else{
$uri_vars = "&".$final_keys[$key]."=".$_POST[$final_keys[$key]];
}
}
And I get this error: Warning: call_user_func(Array) [function.call-user-func]: First argument is expected to be a valid callback in /.........testreqmeta.php on line 91
I'm still not getting any errors (other than the undefined $key in the second sample) when trying the sample code, so I can't say for certain what will fix it, but here's an approach that simplifies things and gets rid of the array indexing operation: use a foreach loop rather than a for loop.
$query = array();
foreach ($final_keys as $key) {
if(!$regexp->$key($_POST[$key])) {
$invalid_uri_vars[] = $key;
} else {
$query[] = "$key={$_POST[$key]}";
}
}
$uri_vars = implode('&', $query);
I've also replaced the repeated string appends with an array implosion, which should be slightly more performant. It also makes it easier to further process the query string, if necessary, before submitting it. There are better approaches yet, but that's a whole other topic.
NB. RegExp isn't a very good description of what the class does, hence it isn't a very good name. The class may use regular expressions, but it isn't itself a regular expression (which support operations such as "match" and "replace"; RegExp's API has none of these).
Quoting from your link:
ok, here is some workaround on this problem:
in case of
<?
class Test {
var $var = Array('test_function');
function test_function($echo_var) {
echo $echo_var;
}
}
$test_obj = new test;
$test_obj->var[0]('bla');
?>
you can avoid Fatal error in last string using this instead:
<?
$test_obj->{$test_obj->var[0]}('bla');
?>
So then:
if($regexp->{$final_keys[$index]}($_POST[$final_keys[$index]])){
There's a function for what you are trying to achieve call_user_func:
http://de2.php.net/manual/en/function.call-user-func.php

Categories