Solved

assigning a var from an array

Posted on 2014-11-23
6
130 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 110

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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 a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

710 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