You haven’t mentioned where you’re running display(), or where you’re expecting the output (echo) to display. I can only assume you want it at the top of the page. Let me explain how to use this.
Basically the way this works, is that wherever you run display(), is where the echoes will output (in this case, before the body). Wherever you echo $avatar is where the id will be displayed. I hope this works for you.
Any variable used inside a function is by default limited to the local function scope.
To fix this problem, use the global keyword. This way you will explicitly set $avatar; as a global variable so it can be used outside of your display() function.
Here is how it would work for your example:
function display() {
global $avatar;
// Rest of your display() function
// Set $avatar somewhere in here
}
display(); // Actually call display so $avatar is set
<div id="avatar-div" style="height:100px; width:100px;">
<img src="<?=$avatar;?>">
</div>
Problem :
I am making a tool to convert steam profile urls to the different steam ids, whatever.
I have the different functions, triggered when submitting the URL of the profile with a form.
One of the function are, that it gets the avatar of the regarding steam profile.
Actually it gets the link of the image. What I can do is, echo an img element with the link, this wont give me any flexibility since I wont be really able to style etc that afterwards.
Now, the function of getting the avatar, is in the function of the submit form.
I tried just inserting the image URL into an img element.
<img src"<?php echo $avatar ?>">
Well, then I get a error message, saying “$avatar” is undefined, even though I defined it in my main php tags. I think that is due to the fact that it is done inside the form function, could be wrong though.
My main question now is, what could be a different approach to this? I need that in the form function because it should only be called then.
Maybe I just have to use the inconvenient way of echoing an img element every time.
Doesn’t look like you’re saving the avatar variable in the global scope anywhere. You could return the avatar value from your function or define
Comment posted by Bijan
Your posted code has 0 instances of
Comment posted by rexsurrection
@Bijan i edited it, its “$avatar” everywhere, just messed it up, when explaining.
Comment posted by EternalHour
You haven’t provided the needed information for someone to give you an answer. Is your HTML and PHP on the same page? Where do you execute
Comment posted by rexsurrection
The HTML and PHP is on the same page, the “display()” is being executed when submitting a form. Currently not sure how to set the variable scope so it is global.
Comment posted by rexsurrection
Ok, thank you for this answer, this would work now, but it just inserts every information into the img (all other things in the if statement). Just putting the rerturn after “$avatar” gets assigned wont work either, since it will just stop with the if statement there, and the other information wont be displayed. How could I fix that?
Comment posted by EternalHour
The problem is that you’re trying to do too much within the same function. Each function should be simple and perform a specific task. You should have one function to process
Comment posted by rexsurrection
Ok, but waht you are supposing doesnt really make a difference, right? I mean your just making an extra variable or am I misunderstanding something?
Comment posted by EternalHour
The best approach with what you have may be using a global, although I’ve never written code that way and don’t agree with it.
Comment posted by EternalHour
It makes a difference because you could determine what
Comment posted by rexsurrection
Ok, so could I do it like, global $avatar = …… or define it first and then make it global. Because I used the 2nd method and the error still occurs.
Comment posted by Bijan
You do not have to set
Comment posted by stemconvtest.000webhostapp.com/test/index.php
Sadly this still doesnt work, adding it in, when the profile isnt found, results in nothing being displayed. And if I set it as a global at the top, will sill result in it saying, that it isnt defined. Here is a link of the project. If you open the image icon in a new tab, you can see the error message in the url.[link]