1

I am trying to convert a string into a variable. I know that this question has been already asked, but I can't get it to work. I need help. I have a few variables that I get through POST like this:

$name = $_POST['name'];
$prename = $_POST['prename'];

$array_var = array("name","prename");
$array_var_count = count($array_var);       

for($t=0;$t<$array_var_count;$t++){
    $var="$".$array_var[$t];
    //echo("<script>alert('".$var."');</script>");
}

Now $var should get the value of :first:(name) and second of:(prename) because $var==("$".$array_var[$t])=> $var=$name;. This means it should take the value of $name = $_POST['name'];. Yet it doesn't. This is why I think I have to convert it into a variable. I'm not quite sure. I'm new in php. And I also want to check if $var is not empty, like this:

for($t=0;$t<$array_var_count;$t++){
    $var="$".$array_var[$t];
    ..............................
    if(!empty($var)){
    ..............................
    }
}

1 Answer 1

2

Your script has some issues. If I understand correctly, this might suit you:

$array = array(
    "first_name" => $_POST['first_name'],
    "last_name" => $_POST['last_name']
);  

foreach($array as $key => $value){
    if($value != ''){
        echo "<script>alert('$key = $value');</script>";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@splash58 Doh! Sorry, I use JS all day long... Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.