susanBuck / e2-fall22

0 stars 0 forks source link

Call variable from within variable #16

Closed ghost closed 2 years ago

ghost commented 2 years ago

Hi,

If I store a variable inside another variable in index file, how do I call it from view file, I'm trying the following:

view file:

$imagePath = '/images/example.png';`
if(statment){
$result="$imagePath"}`

index file:

<img src='<?php echo $result>'>

Thank you

susanBuck commented 2 years ago

This line is missing a closing PHP tag:

<img src='<?php echo $result>'>

It should be:

<img src='<?php echo $result ?>'>

If that doesn't resolve your issue, let me know more about how it's failing. For example, is the image just not loading or are you getting an error message? If the latter, what is the error message?

Also, it's always helpful to include links to the relevant files in your Github repository so we can see exactly what you're working with.

susanBuck commented 2 years ago

One more thing: when setting the $result to $imagePath, do not put $imagePath in quotes.

It should look like this:

if(statement) {
    $result = $imagePath;
}
ghost commented 2 years ago

thank you

traveler9878 commented 2 years ago

This is a good question. The things you may want to start thinking about in this case are the scope of the variable and if it is called/passed/assigned by value or by reference. If this question gets you to the point where you understand the questions above that's a big win at this point. They're starting to introduce these concepts gradually now and I'm sure we'll have a handle on them in full before the course is over.

ghost commented 2 years ago

Thank you, I'm excited to learn more about it from course's materials