John Davidson

arrays - PHP Variable undefined for eval function

0 comments
Message:


I'm new to PHP and coding in general.
Currently I'm trying to make a Simple Calculator.
For this I have the two scripts calculator.php and array.php
My Idea is to store all the inputs the user gives in an array so I can calculate them using the eval() function. Because I need a string for this I converted the array using a foreach loop.
But when I try to use the variable outside the foreach loop I get the following errors:


Notice: Array to string conversion on line 17
Array
Parse error: syntax error, unexpected ';', expecting '(' in /Applications/XAMPP/xamppfiles/htdocs/dashboard/array.php(22) : eval()'d code on line 1


What can I do to fix this problem?


array.php


<?php

session_start();

if (!is_array($_SESSION)) {
$_SESSION['persistentValues'] = array();
}

if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}



foreach ($_SESSION as $value) {
echo $value;
}


if (isset($_POST["result"])) {
$p = eval($value.";");
echo $p;
}

?>

calculator.php


<?php

include ('./array.php');

?>


<div style="padding-left:40px">
<h2> PHP Calculator</h2>
<form method="post" action="">

Enter value: <input type="text" name="value"> <br> <br>
<div style="padding-left: 105px">
<input type="submit" value="9" name="button">
<input type="submit" value="8" name="button">
<input type="submit" value="7" name="button">
<input type="submit" value="+" name="button">
<br>
<input type="submit" value="6" name="button">
<input type="submit" value="5" name="button">
<input type="submit" value="4" name="button">
<input type="submit" value="-" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="3" name="button">
<input type="submit" value="2" name="button">
<input type="submit" value="1" name="button">
<input type="submit" value="/" name="button" style="padding-left: 9px">
<br>
<input type="submit" value="0" name="button" style="padding-left:33px">
<input type="submit" value="." name="button" style="padding-right:9px">
<input type="submit" value="x" name="button" style="padding-left: 7px">
<br>
</div>
<br>
<input type="submit" value="Calculate" name="result">
</form>
</div>


My Answer:
The issue in your code is that you are trying to use the variable $value outside of the foreach loop where it is defined. The $value variable is only available within the scope of the foreach loop.

To fix this issue, you can store the values from $_SESSION['persistentValues'] in a separate variable before using it in the eval() function. Here's an updated version of your array.php file:

php

session_start();

if (!is_array($_SESSION)) {
$_SESSION['persistentValues'] = array();
}

if (isset($_POST['button'])) {
$_SESSION['persistentValues'][] = $_POST['button'];
}

$values = implode("", $_SESSION['persistentValues']);

echo $values;

if (isset($_POST["result"])) {
$p = eval($values.";");
echo $p;
}

?>


In this updated code, the values from $_SESSION['persistentValues'] are stored in the $values variable using the implode() function. This variable is then used in the eval() function to perform the calculation.

Make sure to also update your calculator.php file to include the correct path to array.php:

php

include ('array.php');

?>


PHP Calculator




Enter value:


































With these changes, your code should now work as expected and perform the calculations using the values stored in the $_SESSION['persistentValues'] array.

Rate this post

3 of 5 based on 4116 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us