I want to use a variable outside my foreach. how can I solve that in PHP. How can I use $ID outside the foreach?
foreach($row as $key => $value) {
$ID = $value['Id'];
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason for this is that if the for loop is never gone through (that is, $row was an empty array) $ID would never be set. Therefore,
$IDhas to be set beforehand or else it won’t be in scope at all and if you try to access$ID‘s value, you will hit errors.