₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,325,102 members, 8,420,340 topics. Date: Thursday, 04 June 2026 at 04:54 PM

Toggle theme

[php] - Problem! Object Oriented. Accessing Variable In Function A In Function B - Webmasters - Nairaland

Nairaland ForumScience/TechnologyWebmasters[php] - Problem! Object Oriented. Accessing Variable In Function A In Function B (1026 Views)

1 Reply (Go Down)

[php] - Problem! Object Oriented. Accessing Variable In Function A In Function B by Mobinga(op): 2:47pm On Jul 15, 2011
I have two functions.


<?php
function theme_user_header(){

$meh = "sdad".$bleh."sdad";

}
?>


and


<?php
function theme_user_butt(){

//something here

echo $meh

}
?>


I want to access variable $meh which is declared in the first function, in the second function.
However, when I echo/print_r $meh from the second version it returns 0.

I know I'm doing something wrong. This has been bugging me for days!

Thanks.

cool
Re: [php] - Problem! Object Oriented. Accessing Variable In Function A In Function B by Mobinga(op): 2:49pm On Jul 15, 2011
No Globals!
Re: [php] - Problem! Object Oriented. Accessing Variable In Function A In Function B by DualCore1: 3:38pm On Jul 15, 2011
In my opinion, won't work. Your function must return something.
Re: [php] - Problem! Object Oriented. Accessing Variable In Function A In Function B by quadrillio(m): 3:50pm On Jul 15, 2011
@OP

since you don't want to a create class and do the GLOBAL var stuff,

Why not make the function return a variable, something like this should do the magic.

<?php
function theme_user_header(){

$meh = "sdad".$bleh."sdad";

return $meh;

}
?>


and


function theme_user_butt(){

//call the function and echo it
//something here

echo theme_user_header()."testing";

}


now
you need the final piece that will show the final output and that is by calling 'themw_user_butt()'


theme_user_butt();


if all code is meant to be in one page here is what u should have


<?php
function theme_user_header(){

$meh = "sdad".$bleh."sdad";

return $meh;

}

function theme_user_butt(){

//call the function and echo it
//something here

echo theme_user_header();

}

theme_user_butt();
?>



I hope this helps

- SAFE.
1 Reply

Can You Solve This PHP Problem??Procedural Vs Object Oriented PHPPhp Problem: Retrieving Images And Display In Horizontally Format.234

Share Ur Twitter HandlesPls I Need Joomsocial Or Any Social Network Development Tool!Download Eleven40 Blogger Template By Lord HTML