473,372 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Dynamic Variable Names

I have a shopping cart page that allows a user to type in a new
quantity and press a [Submit] button. The quantities entered are in form
text boxes with the names "item302", "item7812", etc. reflecting which
item that particular textbox is associated to. I store the entered
variables using 'import_request_variables("p","p_")' so I end up with
$item302, $item7812, etc. My script looks for these variables by looking
through the shopping cart array which stores the item numbers themselves
and then checks the value of $item<whatever>.

If I have a number stored in $item is there any easy way to tell PHP
to check the value of variable $p_item<item>?

For example, if $item is '302' how can I tell PHP to evaluate $p_item302?

Thank you!

- Michael J. Astrauskas

P.S. I'm currently doing this using the eval() function, but I don't
like that technique.

Jul 17 '05 #1
4 38135
Michael J. Astrauskas wrote:
For example, if $item is '302' how can I tell PHP to evaluate


$varname = sprintf("p_item%d", $item);

EMail:<0110001100101110011000100111010101110010011 010110
11001010100000001100011011100100110000101111010011 011100
11000010111001000101110011000110110111101101101001 00000>
Jul 17 '05 #2 wrote:
Michael J. Astrauskas wrote:
For example, if $item is '302' how can I tell PHP to evaluate

Try to define a dynamic Var:

$item = "ABC";

${"p_item{$item}"} = "value of this var";

print $p_itemABC;

would output "value of this var";

greetings .. timo

Jul 17 '05 #3
"Michael J. Astrauskas" <tr****@cox.net> wrote in message

if $item is '302' how can I tell PHP to evaluate $p_item302?

Use a variable variable:

$name = 'p_item' . $item;
$value = $$name;

Since $name evaluates to 'p_item302', $$name must evaluate
to $p_item302. See the Manual for details:


Jul 17 '05 #4
Nikolai Chuvakhin wrote:
Michael J. Astrauskas" wrote:
if $item is '302' how can I tell PHP to evaluate $p_item302?

Use a variable variable:

$name = 'p_item' . $item;
$value = $$name;

Thank you! I combined what you and Timo Henke suggested and it works

Here is my final code:
$v = ${'p_ITEM'.$key};

- Michael J. Astrauskas

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jas | last post by:
Hi All I guess I should just have tried this to see if it works but I don't have access to a machine running IIS and can't test it and I am itching for an answer. I want to use dynamic...
by: firegun9 | last post by:
Hello all, After looking for the solution for a while in the group, I know this is not possible in C++. So I just say my problme here. The input file is: dev1 1 1 dev2 0 0 0 6 There is a...
by: Tim.D | last post by:
People, I've ventured into the wonderful world of Stored Procedures. My first experience has been relatively successful however I am stuck on using host variables to specifiy actualy table or...
by: swarsa | last post by:
Hi All, I realize this is not a Palm OS development forum, however, even though my question is about a Palm C program I'm writing, I believe the topics are relevant here. This is because I...
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
by: Mark S. | last post by:
As I understand it, C# doesn't offer dynamic variable names. Below is my attempted workaround. Is what I'm doing possible? FYI, I already read all the "why in the world do you need dynamic...
by: Rolf Mander | last post by:
Hi, I need to use dynamic variable names but for objects. As you know something like that works fine: $variable="content"; $part="able"; echo ${"vari".$part}; // gives out content but i...
by: DavidSeck.com | last post by:
Hi guys, first post :) my question: is it possible to have dynamic variable names, I mean something like this: for($i=0;$i<x;$i++){ $y_$i = blabla; }
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also...
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.