/    Sign up×
Community /Pin to ProfileBookmark

simple question from a newbie

Hello,

i declare a variable some place in a php file as follows
$data[‘name’] $this->language->get(‘name’);

and just below i try to show it
echo $name ;

i get this error : undefined variable work

i change the declaration as follows

$name =$this->language->get(‘name’);

i works .

But code has to work with $data keyword .. (i try to modify a framework called Opencart)
what is that i do wrong with $data ??

Thank you..

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 02.2015 — This:
[code=php]
$data['name'] $this->language->get('name');
[/code]

makes absolutely sense. Obviously you tried to re-type if from your code to post here and goofed. OR your actual code has the same goof in it.

Therefore this code:
[code=php]
echo $name ;
[/code]

makes no sense either since there is no variable called $name that we can see here.

Note:

When posting on a forum for help it really requires one to ensure that the posted code is what you are actually using rather than re-typing it. Can't tell you how many times I've had a poster eventually admit that they inadvertently copied something incorrectly and made us work at solving something that wasn't the case.
Copy linkTweet thisAlerts:
@NogDogDec 02.2015 — You have to echo whatever it is you assigned it to:
[code=php]
$data['name'] = $this->language->get('name');
echo $data['name'] ;
[/code]
Copy linkTweet thisAlerts:
@gokselcauthorDec 04.2015 — Thanks ginerim , i'll work on it considering your your warnings.





This:
[code=php]
$data['name'] $this->language->get('name');
[/code]

makes absolutely sense. Obviously you tried to re-type if from your code to post here and goofed. OR your actual code has the same goof in it.

Therefore this code:
[code=php]
echo $name ;
[/code]

makes no sense either since there is no variable called $name that we can see here.

Note:

When posting on a forum for help it really requires one to ensure that the posted code is what you are actually using rather than re-typing it. Can't tell you how many times I've had a poster eventually admit that they inadvertently copied something incorrectly and made us work at solving something that wasn't the case.[/QUOTE]
Copy linkTweet thisAlerts:
@gokselcauthorDec 04.2015 — Thanks NogDog.

Actually i declared this variable in a php file and call it from a template file.

Variable works in php file but template file. Its clear that i need to work on php rules a lot .

Thank you.
Copy linkTweet thisAlerts:
@NogDogDec 04.2015 — In case this has anything to do with it, remember that variables defined within a function are scoped locally only to that function. If you need access to them outside of that function, then the function needs to return it so that you can assign it to a variable...
[code=php]
$myVar = someFunction();
[/code]

(There are some other ways around it, such as passing variables by reference to the function or using the "global" keyword in the function; but that can get a bit messy, and the latter option of using "global" is highly frowned upon as it makes code difficult to debug and/or to re-use.)
Copy linkTweet thisAlerts:
@gokselcauthorDec 05.2015 — Thanks again NogDog,

Actually i was trying to upgrade a module written for Opencart 1.5.5.1 framework to run in Opencart 2.1.0.1.

Problem was that a variable declared a php file could not called from a tpl file with $variablename

error was "notice : variable not defined in "somedir/file.tpl" .

its resolved with changing the statement

[code=php]
$this->response->setOutput($this->load->view('somedir/file.tpl',[B] $this->data[/B]));

[/code]

to
[code=php]
$this->response->setOutput($this->load->view('somedir/file..tpl', [B]$data));[/B]

[/code]


at the end of the php file which the variable was declared.

This modification allowed the variable to be called from tpl file and problem is over now .

No idea if its caused by framework version or something else . ?

Thank you.

Greetings.








In case this has anything to do with it, remember that variables defined within a function are scoped locally only to that function. If you need access to them outside of that function, then the function needs to return it so that you can assign it to a variable...
[code=php]
$myVar = someFunction();
[/code]

(There are some other ways around it, such as passing variables by reference to the function or using the "global" keyword in the function; but that can get a bit messy, and the latter option of using "global" is highly frowned upon as it makes code difficult to debug and/or to re-use.)[/QUOTE]
×

Success!

Help @gokselc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...