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
Solved

assigning a var from an array

Posted on 2014-11-23
6
126 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 109

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

829 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