Solved

assigning a var from an array

Posted on 2014-11-23
6
128 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
Independent Software Vendors: 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

Technology Partners: 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!

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

749 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