Variabale value is being ignored?

I have the following code in a file called test.php.

The issue is the variable ‘$matches’ starts off as an empty string; then gets used inside the concatenated second value of the ‘$test’ array.

Next ‘$matches’ gets redefined as as the optional ‘matches’ parameter inside the preg_match function inside the ‘foreach’ loop.

However, when ‘$test[1]’, which is from inside the ‘foreach’ loop and is concatenated values of matches, is var_dumped; it is still a empty string.

Why is that?


<?php
echo '<pre>';

$test_text = 'big cat sat on a hat';

$matches = ''; //empty string for now

$test[] = array('big (cat) (sat) on a (hat)', $matches[1].' '. $matches[3]. ' '. $matches[2]);

//var dump 1
var_dump($matches, $test[0][0], $test[0][1]);

/***************************
expectation of var dump 1 is:
string(0) ""
string(26) "big (cat) (sat) on a (hat)"
string(2) "  "

This is what var_dump 1 produces

****************************/

foreach($test as $test)
	{
		
		$match_this = preg_match('/'. $test[0].'/', $test_text, $matches);
	
		//var_dump 2
		var_dump($match_this, $matches, $test[1]);

/***************************
expectation of var dump 2 is:

int(1)
array(4) {
  [0]=>
  string(20) "big cat sat on a hat"
  [1]=>
  string(3) "cat"
  [2]=>
  string(3) "sat"
  [3]=>
  string(3) "hat"
}
string(2) "cat hat sat"

instead this is produced in var_dump 2:

int(1)
array(4) {
  [0]=>
  string(20) "big cat sat on a hat"
  [1]=>
  string(3) "cat"
  [2]=>
  string(3) "sat"
  [3]=>
  string(3) "hat"
}
string(2) "  "

NOTE that $test[1] is a empty string even though
$test[1] should equal $matches[1].' '. $matches[3]. ' '. $matches[2]
and the variable $matches is redefined in the preg_match function.



***************************/


	}

?>

What you suggested solved one problem! I changed $V2 to $v2. Now $v2 outputs the value it should.

What puzzles me still is if I define $v3 = $v1 + $v2.
Next change either value of $v1 or $v2, without redefining $v3.

Then $v3 will retain the original sum of $v1 and $v2.
Not the the new value of $v3 that would result from the the sum of the new values of $v1 and $v2.

$v3= $v1 + $V2;

Variables are case sensitive I’m fairly sure. Maybe change the $V2 to $v2 ?

You’ve set $matches as an empty string, but are using it in the form of an array?(from what I gather), try : $matches = array();

Also, when you are assigning $matches within $test, you are calling values within the $matches array ($matches[1]), but it is empty, $matches[1] will not produce anything?

Here is a clearer example.


<?php

echo '<pre>';

$v1 = 10;

$v2 = $v1;

$v3= $v1 + $V2;


var_dump($v1, $v2, $v3);

$v2 = 100;

echo '<br /><br /><br />';

var_dump($v1, $v2, $v3);


?>

Shouldn’t the expected out be:


int(10)
int(10)
int(20)



int(10)
int(100)
int(110)

Instead the output is this:


int(10)
int(10)
int(10)



int(10)
int(100)
int(10)

It seems that $v3, being the sum the sum of $v1 and $v2, ignores the value of $v2. Thus the value of $v3 is 10 instead of 20 in the first var_dump.

When the value of $v2 is changed to 100 before the second var_dump; the value of $v3 being them sum of $v1 and $v2 is still 10 instead of 110.

Thats because you are simply reassigning the values of $v1 and $v2. You will need to reassign the value of $v3 to notice the change. You cannot do this:


$v1 = 10;
$v2 = 20;

$v3 = $v1 + $v2;

//now you are reassigning the values, after the $v3 assignment has taken place

$v1 = 20;


The new $v1 will have no affect on the previous $v3 as that assignment operation has already been done. If you wish for $v3 to take into account the new $v1, then you will have to reassign it.

I hope this is not too confusing.