LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2007, 08:59 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
Calling a PHP function from another file returns a weird value


Hello everybody,

I have been struggling to solve a simple problem for more than 4 hours.

I have a php file which is called convertor.php that contains a function to convert some date. I have another file called show.php which contains a function called show_date().

show_date() calls the function inside convertor.php and returns an integer.

When I call show_date() from the same file, everything is correct, but when I call this function from another file, it gives me a weird number.

I found a very interesting situation which looks like this:

show.php

PHP Code:
include_once 'convertor.php';
function 
show_date()
{
    ....
    ...
    
call some function in convertor.php
    
return $x;


$f=show_date();
print 
$f
everything is correct. Now let's have a look at my other file.

other_file.php

PHP Code:
include_once 'show';
$h=show_date();

print 
$h
it gives me wrong answer. It also shows me the result of calling show_date() inside show.php incorrectly. As I know, when I include a php file and call one of its functions, it shouldn't run anything other than called function.

I will really appreciate if you let me know how I can fix this problem.

Thanks.
 
Old 07-27-2007, 01:06 PM   #2
cconstantine
Member
 
Registered: Dec 2005
Distribution: RedHat, Ubuntu
Posts: 101

Rep: Reputation: 15
The entire file is parsed when you include, or require, a file.

So in this case, your function gets defined, and
Code:
$f=show_date();
print $f;
gets executed by the include. If your function in convertor.php has side effects... that would explain the diff you're seeing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a function from within another function in php jayakrishnan Programming 2 06-19-2007 08:36 AM
Function that returns the size of a file in bytes in C kalamaraki Programming 2 01-07-2007 11:22 AM
weird PHP problem: Call to undefined function mysql_connect() Shioni Linux - Server 4 12-22-2006 05:26 AM
Weird PHP error: Cannot create MySQL database from a function?!!? JockVSJock Programming 5 09-28-2006 08:46 PM
Calling a Php file from CRON every hours ? airbuzz *BSD 10 02-03-2004 02:04 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration