Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Substring headaches Re: Substring headaches

#114512
__
Participant

> still it doesn’t load. Chrome states “HTTP 500 Internal Server Error.”

some hosts are more zealous about hiding errors. Ask them about it. Also ask about where your error log file is; PHP errors should be being logged there.

> $wares does exist, …

Not in your function, it doesn’t.

Read up about “scope” in PHP. Variable scope is, basically, the “space” within a script where variables “exist.” Classes and functions in PHP have their own scope. Example:

# this is the “global scope.”
# if you declare $myvar here,
# it’s available anywhere in the global scope.

if( condition ){
# conditional blocks, loops, etc. don’t have their own scope
# $myvar is available here.
}

function myfunc(){
# functions *do* have their own scope –
# $myvar does not exist here!

$myvar = “other value”;
# if you declare $myvar inside the function,
# it **is not** the same as the $myvar in the global scope.
print $myvar;
# local (myfunc) scope – prints “other value”
}
print $myvar;
# global scope – prints “some value”

To use a global variable inside a function, you need to either pass it in (preferred), or import it from the global scope.

## preferred ##
function pass( $arg ){
/* do stuff */
}
# when you pass $myvar to the function, like so –
pass( $myvar );
# it is available inside the function as “$arg”.

# import the variable you need into the function
function import_var(){
global $myvar;
# now the global $myvar is available inside the function.
/* do stuff */
}
# this method is less preferable,
# since you cannot be sure of the current state of the global $myvar
# (or if it even exists when the function is called)

> I want two functions: 1. takes an array (just values array, no keys), cycles through them, finds their key in the $wares array, and appends it to a string. The wares are separated by a period. So you get a string like 1.4.28 that I can store in the database. 2. takes the string from the database and reverts the string back to an array.

Before going ahead with this, consider that you’re defeating the purpose of using a database by encoding a bunch of data and storing it in a single field – you’re effectively making it “invisible.”

What if you wanted to find records of Oranges (“1”)? You’d have to extract all the data, convert it back into an array, and *then* search for Oranges. If you have a large dataset, that’s a huge waste of time. If you’d stored “Oranges” in the database directly, MySQL could’ve searched *for* you (that’s its purpose in life, after all).

But anyway,

If you want to represent an array as a string (e.g., so you can store it in the DB), there’s already a function to do that:

$array = array( ‘oranges’,’apples’,’pears’ );

# convert $array into a string value
$str = serialize( $array );

# now you can save it to the DB, in your SESSION, whatever
# to convert it back, use:
$array = unserialize( $str );

# you can also use JSON –
$jsonstr = json_encode( $array );

# and convert it back into an array
$array = json_decode( $jsonstr,true );