Solved

assigning a var from an array

Posted on 2014-11-23
6
115 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

706 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

20 Experts available now in Live!

Get 1:1 Help Now