Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,625 members, 7,955,287 topics. Date: Saturday, 21 September 2024 at 09:39 PM

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

Nairaland Forum / Science/Technology / Webmasters / [php] - Problem! Object Oriented. Accessing Variable In Function A In Function B (972 Views)

Can You Solve This PHP Problem?? / Procedural Vs Object Oriented PHP / Php Problem: Retrieving Images And Display In Horizontally Format. (2) (3) (4)

(1) (Reply) (Go Down)

[php] - Problem! Object Oriented. Accessing Variable In Function A In Function B by Mobinga: 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: 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)

Foriegn Host / Nigerian Web Hosting Company..which Is Better? / How Can I Receive Payment From Customers Through Sms / I Need Someone To Partner With My Blog... On A 50/50 Sharing Deal.

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 9
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.