Hi,

I am a little confused as how to do this with PHP.

I would like to create some variables on the fly within a While loop and have this so far.

<?php
$extended  = "3^3^4^4^1|rrsrw|3|2|mth_scrn|1|3";
					$extCount = count($pieces = explode("|", $extended));
					$y = "1";
					$z = "0";
					$varName="ext_";
					while($y <= $extCount){
						// I need to create new variables call $ext_ with number appended to the end
						$varName eval($y) = $pieces[$z]; // How do I create this variable on the fly?
					$y++;
					$z++;
					}
?>

I used the EVAL for this purpose with Flash Actionscript (I think!) but cant figure out the correct syntax in PHP - and the documentation has confused me!!!

Any pointers would be much appreciated,
GiantComm...

    The best way to work with eval() is to start with what you want to declare:

    eval(   $ext_1 = '3^3^4^4';  );  //what i think you want but you can go from here

    Now turn that into a string with variables available:

    eval(   '$ext_' . $y . ' = \'' . $pieces[$z] . '\';'  );

    and there you have it. From my experience with eval(), the ending semicolon is not needed for the expression to evaluate unless you have multiple commands or logical expressions. Also, be careful with your quotes.

    I'm looking at your code and your coding is fairly well advanced, for example you're doing two things at once with count($pieces = explode('|',$extended)) - I'd never even thought of that! :-) it seems that you might however be better served using arrays in some fashion to parse the string.

    Samuel

    GiantComm wrote:

    Hi,

    I am a little confused as how to do this with PHP.

    I would like to create some variables on the fly within a While loop and have this so far.

    <?php
    $extended  = "3^3^4^4^1|rrsrw|3|2|mth_scrn|1|3";
    	$extCount = count($pieces = explode("|", $extended));
    	$y = "1";
    	$z = "0";
    	$varName="ext_";
    	while($y <= $extCount){
    		// I need to create new variables call $ext_ with number appended to the end
    		$varName eval($y) = $pieces[$z]; // How do I create this variable on the fly?
    		$y++;
    		$z++;
    	}
    ?>
    

    I used the EVAL for this purpose with Flash Actionscript (I think!) but cant figure out the correct syntax in PHP - and the documentation has confused me!!!

    Any pointers would be much appreciated,
    GiantComm...

      while ($y <= $extCount) {

      $varName = $varName . $y;
      $$varName = $pieces[$z]; // This uses the string inside of $varName (ext_123) as a variable name

      }

        $extended = '3^3^4^4^1|rrsrw|3|2|mth_scrn|1|3';
        $explode = explode('|', $extended);
        foreach ($explode as $key => $value)
        {
        	$GLOBALS['ext_' . $key] = $value;
        }
        
        echo $ext_1; // outputs "rrsrw"
        

          Hi,

          If you really need to actually create variables on the fly (in 15 years of programming, I've never had to do it) then you could do ...

          <?php
          $extended  = "3^3^4^4^1|rrsrw|3|2|mth_scrn|1|3";
          $extCount = count($pieces = explode("|", $extended));
          $y = 1;
          $z = 0;
          $varName = "ext_";
          while($y <= $extCount){
          	$GLOBALS["ext_".($y++)] = $pieces[$z++];
          }
          
          echo $ext_1;
          ?>

          EDIT: Just seen devinemke's answer!!

          But, it shouldn't be necessary in PHP, because you could just use the $pieces array instead ...

          <?php
          $extended  = "3^3^4^4^1|rrsrw|3|2|mth_scrn|1|3";
          $pieces = explode("|", $extended);
          echo $pieces[0];
          ?>

          Paul 🙂

            Thanks very much sfullman - that works a dream.

            Actually the data i am parsing is from part of a much larger array created from an XML file. Unfortunatley I dont have any control over the XML source as it is sent remotely from a handheld device!

            $extCount = count($pieces = explode("|", $extended));
            $y = "1";
            $z = "0";
            while($y <= $extCount){
            	eval('$ext_' . $y . ' = \'' . addslashes($pieces[$z]) . '\';');
            $y++;
            $z++;
            }

            I dont suppose there is a way of making this piece of code shorter is there?

            Thanks again, much appreciated,
            GiantComm...

              GiantComm wrote:

              I dont suppose there is a way of making this piece of code shorter is there?

              i think my example above is a bit shorter

                Thanks for everyones input on this.

                What I didnt mention was that the number of elements that I may need to extract from $extended can and will vary from 3 too 10 depending on the course!

                Thats why I wanted to create the $ext_ dynamically. Soory for not pointing that out in the first place - but it is working just fine now.

                Thanks,
                GiantComm

                  Write a Reply...