Solved

assigning a var from an array

Posted on 2014-11-23
6
120 Views
Last Modified: 2014-11-24
Ok, I have an example like this:

echo "{$geoplugin->city}";

and that works and displays the city name.

However, I don't want to display it, I want to use it in a variable, like this:

$city = {$geoplugin->city};

but.... if I do that with the brackets on it (I don't know what those do in the example) I get a php error and the page won't load.

If I don't use them, I don't get any results for $city.  If I try to echo $city, I get nothing.

How do I do this?   (the script I am basing this on is here: http://www.geoplugin.com/_media/webservices/geoplugin.class.phps?id=webservices%3Aphp&cache=cache )

Thanks for any assistance -

Chris
0
Comment
Question by:St_Aug_Beach_Bum
  • 3
  • 2
6 Comments
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40461199
All you should need is

$city = $geoplugin->city;

The curly brackets are only there because you are embedding it in a string and PHP needs to know the whole thing is a variable else it will only ready $geoplugin and ignore the rest.
So if it isn't working then it likely means you are calling it before the class is initiated.
0
 

Author Closing Comment

by:St_Aug_Beach_Bum
ID: 40461321
Thank you Gary.

Your answer convinced me my problem must have been somewhere else - finally realized I had not declared $city global where I needed it.

Ok, fixed now, and the rest of the script can wait until I come back and take a fresh look after watching The Walking Dead! :)  Sometimes I go a bit zombie-like (brain dead) after looking at code too long...

Thanks again,

Chris
0
 
LVL 58

Expert Comment

by:Gary
ID: 40461368
Just about to watch it, past few episodes have been boring as hell.
Z Nation is a guilty pleasure.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:St_Aug_Beach_Bum
ID: 40461443
Oh - I can't believe you said that!

I've been thinking the exact same thing, I'm ready for TWD to get things moving.... though I guess they are. It doesn't feel like it though. Even tonight's.

And damn, I've been thinking about writing a post on my site saying that exact thing about Z Nation! It's not in the same class as TWD, but it's fun to watch :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40461876
Just a note,,, This is not an array notation, it's an object notation:

$geoplugin->city

You can tell we are dealing with an object because of the -> component in the variable name.  To see what is in there, you use var_dump($geoplugin) and PHP will tell you all of the data elements (called "properties") inside the object.  To see the individual property named city, you use var_dump($geoplugin->city)
0
 

Author Comment

by:St_Aug_Beach_Bum
ID: 40462098
Thank you Ray!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now