Page 1 of 1
passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 9:43 am
by johnmurray0101
have a PHP script which is calling a Silverlight object
Code: Select all
function renderSilverLightControl2($source)
{
echo "<div id='silverlightControlHost'>";
echo "<object width='100%' height='100%' type='application/x-silverlight-2' data='data:application/x-silverlight-2,' >";
// echo "<param name='initParams' value='originatorfname=John,originatorsname=Murray' />";
echo "<param name='initParams' value='originatorfname=${firstName},originatorsname=${lastName}' />";
etc etc
This works fine isf the commented out line is used
Code: Select all
echo "<param name='initParams' value='originatorfname=John,originatorsname=Murray' />";
but fails to pass any parameter if the variables ae substitued as in subsequent line
I have looked everywhere and I can't really see what's wrong with my variable substitution
Yes I've checked variables are there as expected with no odd text or chars
oddly enough when tring to debug I've used 'print' but if the string contains "<" or "/>" one cannot see the string
so I cant get a handle on what's going on
help please -
J M
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 11:54 am
by califdon
You haven't shown the code that uses the parameter value and is causing the problem. If it's the case that what you showed us is all you have written, of course it doesn't work because it's not complete. Instead of the literal "value='originatorfname=John,originatorsname=Murray", you must substitute "value=$source" (assuming that the parameter $source contains "John,originatorsname=Murray".
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 12:20 pm
by johnmurray0101
sorry corrected the previous post
my code which uses teh variable substituion is this
Code: Select all
echo "<param name='initParams' value='originatorfname=${firstName},originatorsname=${lastName}' />";
I have tried with out curly braces and also this
Code: Select all
echo "<param name='initParams' value='originatorfname=$firstName,originatorsname=$lastName' />";
but it only seems to work when I have the values as literals - I have read copiously and I believe this should work so it's obviously a newbies block somewhere
Also oddly this works using a variable
Code: Select all
"<param name='source' value='$source'";
and is part of the same parameter setting lines
it seems to be something about the initParams which require quotation marksin the string
tx for previous quick response and sorry again for my mistake
John
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 12:30 pm
by califdon
It's simply a matter of using single quotes, just as you did in your last example. The reason is that the value is a string, so it must be surrounded by either single or double quotes, and since your entire echo line is in double quotes, you have to use single quotes (or escaped double quotes, but that would be hard to read and unnecessary). It's not about initParams, it's simply that the value is a string. If it were numeric, you wouldn't use quotes. Never use brackets.
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 12:52 pm
by johnmurray0101
tx Califdon (btw love your byline
so if I use single quotes as per literal string this should work?
Code: Select all
echo "<param name='initParams' value='originatorfname=$firstName,originatorsname=$lastName' />";
Unfortunately it doesn't and that' s where I started - readin gup I found that complex expressions can use curly braces so I tried that.
but as I say the original code which according to my reading should work, doesn't - in practice it is no different from the literal string which succesfully passes the initParams
thus - literal string which works =
Code: Select all
"<param name='initParams' value='originatorfname=John,originatorsname=Murray' />";
and of course I've tested the variables are valid strings
should the variables have quotes around them ?
John
ps do appreciate the help - this is the last bit of a big project and it is very frustrating
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 1:03 pm
by johnmurray0101
quick follow on
I tried swapping the types of quote
thus
Code: Select all
echo '<param name="initParams" value="originatorfname=$firstName,originatorsname=$lastName"/>';
this pass the parameters but of course instead of variable substitution I got the parameters as literal $firstName and $lastName :=(
I tried a bit of concatenation thus
thus
Code: Select all
echo '<param name="initParams" value="/"originatorfname=".$firstName.",originatorsname=".$lastName"/""/>';
I'm in danger of inventing my own version of php now
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 1:31 pm
by McInfo
johnmurray0101 wrote:I've tested the variables are valid strings
Did you test them inside or outside the function? In the code you have posted, I don't see how $firstName and $lastName are supposed to exist in the function scope.
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 2:05 pm
by johnmurray0101
Yes - I used print('$firstName') before function is called and it demonstrates that the variables contain the correct content
Ah just had a thought - should the variables be passed to the function as function parameters ?
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 2:39 pm
by Weirdan
johnmurray0101 wrote:Ah just had a thought - should the variables be passed to the function as function parameters ?
The thought worth exploring indeed.
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 2:42 pm
by McInfo
In PHP, variables outside the function (global scope) are not automatically available within the function (except superglobals). You must either declare the variables global from within the function or pass them to the function as parameter arguments.
Try printing the variables from within the function. Actually, use var_dump() so you can see that they are NULL.
Enable display_errors and set error_reporting to a permissive level so you can see the errors that are triggered by attempting to read from variables that are not defined in the current scope.
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 5:03 pm
by johnmurray0101
whooaa yeah - thanks Guys
yes it is the scope of the variables
I've now passed them as function parameters and I have got it to work
So (excuse a cheeky follow on question after all your help

)
how does one declare supervariables - are they similar to 'globals' in vb or static in c#
tx enormously for the help - drinks all round at my place
John
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 5:23 pm
by McInfo
Superglobals are predefined variables that allow you access to things like user input and server environment variables. You can modify and add to the superglobal arrays, but you can't name your own superglobals (unless you modify PHP itself).
Re: passing paramters using variables newbie confusion
Posted: Sun Jul 03, 2011 6:04 pm
by califdon
Here's a guide to the several kinds of variables in PHP:
http://www.tutorialspoint.com/php/php_g ... iables.htm
But be aware of the hazards of using globals improperly. Most common PHP logic can be accomplished without using globals.