473,386 Members | 1,973 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,386 software developers and data experts.

Turning words in string into individual variables

bugboy
160 100+
I have a comma delineated string from an html form:

"wind, sun, ocean, lake"

i can use explode to turn it into an array but what i want to do is take each word and assign it to it's own variable (The number of words in the string may change), to end up with this:

$word1 == 'wind'
$word2 == 'sun'
$word3 == 'ocean'
$word4 == 'lake'

The reason is that I want to be able to put each variable into a single mysql query that cross references all the words at once.

Thanks!
Oct 26 '07 #1
7 2311
Lumpy
69
Could use just put all the words into an array, then just count how many values you have in the array and use each one as you need it? You know what I mean, that is almost what you are doing with $word1, $word2 and so on. Put everything into the $word array and then use count() to see how many you have and then call each one with something like $word[$c].
Oct 27 '07 #2
pbmods
5,821 Expert 4TB
Heya, bugboy.

Combine explode() with extract().
Oct 27 '07 #3
bugboy
160 100+
cool.. Thanks guys!

I've been reading up on extract() and i think i understand what is happening.. but.. what is a "local symbol table"?
Oct 27 '07 #4
pbmods
5,821 Expert 4TB
Heya, bugboy.

A symbol is a variable, function or class name.

In other words, when you use a variable, function or class:
Expand|Select|Wrap|Line Numbers
  1. echo $someVar;
  2.  
PHP looks up the symbol ('$someVar') in its table of existing symbols to find the [location in memory of the] value of the variable with that name.

If the symbol hasn't been defined yet, PHP throws an error of E_NOTICE severity.

There are multiple symbol tables; each represents a scope. For example:

Expand|Select|Wrap|Line Numbers
  1. $global = 'not available to example()';
  2.  
  3. function example( $var1 , $var2 )
  4. {
  5.     $var3 = ' some value ';
  6.  
  7.     return $var1 . $var3 . $var2;
  8. }
  9.  
example()'s symbol table contains $var1, $var2 and $var3, but not $global.
Meanwhile, the global symbol table contains $global, but not $var1, $var2 nor $var3.

Note that this only applies to variables; functions and classes are always available anywhere after they've been defined.
Oct 27 '07 #5
bugboy
160 100+
Wow, thanks pbmods!

So the 'global symbol table' is available to the whole page and a super global is available across multiple pages?

Can i add a value to the superglobal(?) symbol table that has a multi page scope without a form submit?
Oct 28 '07 #6
pbmods
5,821 Expert 4TB
Heya, bugboy.

Not quite. Both superglobals and globals are available to the global scope of every script for a given page load. The difference is that superglobals can be used anywhere, whereas globals are only available in the global scope or by using the $GLOBALS superglobal (or the global keyword).

Here are a couple of examples:

$_SESSION is a superglobal:
Expand|Select|Wrap|Line Numbers
  1. echo $_SESSION['key'];    // Works
  2.  
  3. function doSomething()
  4. {
  5.     echo $_SESSION['key'];    // Also works.
  6. }
  7.  
On the other hand, $someVar is just a regular global:
Expand|Select|Wrap|Line Numbers
  1. $someVar = 5;
  2.  
  3. echo $someVar;    // Works
  4.  
  5. function dontDoAnything()
  6. {
  7.     echo $someVar;    // Doesn't work.
  8. }
  9.  
  10. function doSomething()
  11. {
  12.     global $someVar;
  13.  
  14.     echo $someVar;    // Works.
  15. }
  16.  
  17. function doSomethingElse()
  18. {
  19.     echo $GLOBALS['someVar'];    // Works; note that $GLOBALS is a superglobal.
  20. }
  21.  
  22. function orYouCouldDoThis()
  23. {
  24.     $someVar =& $GLOBALS['someVar'];    // Note that this is identical to "global $someVar;"
  25.  
  26.     echo $someVar;    // Works.
  27. }
  28.  
Oct 28 '07 #7
bugboy
160 100+
Thanks pbmods, i really appreciate all your help and explanations. It helps immensely with not only what to do but why.

BugBoy
Oct 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: mr_burns | last post by:
Hi there, I have a text box that I will use to search a database. I would like to use it so that it will not use a whole string (ie. 'red striped top') but instead break it up into individual...
20
by: Brian Kazian | last post by:
Here's my problem, and hopefully someone can help me figure out if there is a good way to do this. I am writing a program that allows the user to enter an equation in a text field using...
10
by: Aaron | last post by:
string a = "i have an apple"; i want the output to be "I Have An Apple"; i was able to capitalize the first word but can't figure out how to capitalize every word in that string Thanks,...
5
by: derrick | last post by:
I used to program text RPG's in C back in high school, but I was very crude at it and only learned what I needed to get the job done. I am now about to graduate college with a degree in English and...
40
by: Alex | last post by:
Hello, does anybody know how to turn off the autocomplete feature for a certain text field? I am aware of the "autocomplete" attribute, but I have seen other implementions achieving it...
3
by: Maya | last post by:
Hello guys, Is there an easy way to extract individual words that form a string and store them in variables like in this example: String "how are you?" My result would be: var1 = "how"
4
by: J | last post by:
anybody know a vb.net command to populate an array with each word in a string? for example " hi how are you" array(0) = hi array(1) = how array(2) = are array(3) = you i know i could...
7
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
3
by: Ramper | last post by:
I need to Write a function that will, given an input string containing many words, split that string into individual words. For each word, the function should output the word, its starting index in...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
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...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.