Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vincent15000's avatar

Variable scope in a condition

Hello,

I have this code.

$errors = [];

foreach ($items as $item) {
	if ($item != $some_test_value) {
		$errors[] = [
			'item_id' => ...,
			'error_message' => ...,
		];
	}
}

print_r($errors);

I think that I have a scope variable problem with the $errors variable. Effectively inside the condition the $errors retrieves the new error, but print_r shows that the array is empty.

How to solve that ?

I specify that the condition works great.

Thanks for your help.

V

0 likes
10 replies
Snapey's avatar

with foreach, nothing is out of scope

1 like
vincent15000's avatar

@Snapey Yes that's true ... I really wonder why my array is empty whereas I do have to retrieve an error.

vincent15000's avatar

@Snapey I see the right values for the item inside the loop. The error is added to the array inside the loop and the condition (checked with print_r), but once out of the condition and the loop, the array is empty.

And if I add the error into the array inside the loop but outside the condition, the error is visible outside the loop.

Very strange ...

newbie360's avatar

@vincent15000 because this line return value is unexpected

if ($item != $some_test_value) {

can you show the full code

1 like
vincent15000's avatar

@newbie360 @snapey Here is the full code. I need this to compare the old code and the new code to check if I obtain the same datas. So I retrieve an object in the databse with its properties, I create another object and I run the calculation with the new code and then I check if both objects have the same values.

// rerefence with ORIGINAL code
$object = new Object($object_id);

// test to check if the new calculation code is ok
$testObject = new Object;
$testObject->calculate();

// now I compare both objects ($object and $test)

$fields = [
	'start_date',
	'end_date',
	'property_1',
	'property_2',
	...
];

$validation = true;

$errors = [];
$errors[] = ['id', 'field', 'old', 'test', 'gap']; // here the idea is to save the errors into a CSV file

foreach ($fields as $field) {
	if ($object->$field != $testObject->$field) {
		$validation = false;

		if ($field == 'start_date' || $field == 'end_date') {
			$gap = null;
		} else {
			$gap = round(($object->$field - $testObject->$field) / $testObject->$field * 100, 1).' %';
		}

		var_dump('ID : '.$object_id.PHP_EOL);
		var_dump('FIELD : '.$field.PHP_EOL);
		var_dump('ORIGINAL : '.$object->$field.PHP_EOL);
		var_dump('TEST : '.$testObject->$field.PHP_EOL);
		var_dump('GAP : '.$gap.PHP_EOL);

		$errors[] = [
			$object_id,
			$field,
			$object->$field,
			$testObject->$field,
			$gap,
		];

		print_r($errors); // IS OK => I SEE THE ERROR ADDED TO THE ARRAY
	}
}

print_r($errors); // IS NOT OK => I DON'T SEE THE ERROR ADDED TO THE ARRAY
newbie360's avatar

@vincent15000 i can't see any code overwrite the $errors, you may try debug it line by line

// rerefence with ORIGINAL code
// $object = new Object($object_id);

// test to check if the new calculation code is ok
// $testObject = new Object;
// $testObject->calculate();

// now I compare both objects ($object and $test)

$fields = [
	'start_date',
	'end_date',
	'property_1',
	'property_2',
	// ...
];

// $validation = true;

$errors = [];
$errors[] = ['id', 'field', 'old', 'test', 'gap'];

foreach ($fields as $field) {
	// if ($object->{$field} != $testObject->{$field}) {
	if (true) {
		// $validation = false;

		if ($field == 'start_date' || $field == 'end_date') {
			$gap = null;
		} else {
			// $gap = round(($object->{$field} - $testObject->{$field}) / $testObject->{$field} * 100, 1).' %';
			$gap = 'gap_number';
		}

		// var_dump('ID : '.$object_id.PHP_EOL);
		// var_dump('FIELD : '.$field.PHP_EOL);
		// var_dump('ORIGINAL : '.$object->{$field}.PHP_EOL);
		// var_dump('TEST : '.$testObject->{$field}.PHP_EOL);
		// var_dump('GAP : '.$gap.PHP_EOL);

		$errors[] = [
			// 'object_id' => $object_id,
			'field' => $field,
			// 'object_field' => $object->{$field},
			// 'testobject_field' => $testObject->{$field},
			'gap' => $gap,
		];

		info('inside', $errors);
        // print_r($errors);
	}
}

info('outside', $errors);
// print_r($errors);
1 like
newbie360's avatar

@vincent15000 with my code, in the log i can see both message, no idea whats wrong @@

you have initialize the $errors variable, i don't know why LOL

1 like

Please or to participate in this conversation.