passing paramters using variables newbie confusion

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

passing paramters using variables newbie confusion

Post 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
Last edited by johnmurray0101 on Sun Jul 03, 2011 12:15 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: passing paramters using variables newbie confusion

Post 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".
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

Re: passing paramters using variables newbie confusion

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: passing paramters using variables newbie confusion

Post 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.
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

Re: passing paramters using variables newbie confusion

Post 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
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

Re: passing paramters using variables newbie confusion

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: passing paramters using variables newbie confusion

Post 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.
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

Re: passing paramters using variables newbie confusion

Post 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 ?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: passing paramters using variables newbie confusion

Post 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.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: passing paramters using variables newbie confusion

Post 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.
johnmurray0101
Forum Newbie
Posts: 6
Joined: Sun Jul 03, 2011 9:33 am

Re: passing paramters using variables newbie confusion

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: passing paramters using variables newbie confusion

Post 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).
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: passing paramters using variables newbie confusion

Post 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.
Post Reply