Jump to content

can't call function


Go to solution Solved by NotionCommotion,

Recommended Posts

I'm writing a program that allows users to input a stock and view a historical price chart. Code is straight out of libchart charting library. Problem is, user is supposed to enter the stock symbol from a form handler (index.php) which then passes the symbol as a variable to the charting function, which doesn't get called at all:

<? php

  function index() { chart($_POST['userinput']};

   }  ?> .

 

<?php

  function chart($stock)

  {

  $prices=getdata($stock); //returns array of prices from yahoo finance

  $data=analyzer($prices); //produces metrics to be charted

  graph($data); } ?> //plots the metrics, outputs .html

 

chart.php works on its own, as I've verified by hardcoding $argv='ibm'; in its code body. But index.php can't seem to call chart.php, as I've verified by including an echo/var_dump line in chart.php (doesn't get executed at all). It doesn't have anything to do with form handling either, as chart('ibm'); doesn't call chart.php either.  I don't understand how a 6-line piece of code can be so problematic. All files are in the same folder, btw.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/
Share on other sites

your first php tag, isn't one. it's a <? followed by a space, then the php. if you look at the view source of your output in your browser, you will see the raw php code since it wasn't seen as being php code on the server and was sent to the browser as is.

Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/#findComment-1497280
Share on other sites

@mac_gyver: sorry, my bad, I didn't copy and paste that code, which I should have. It was so short that I decided to just type it right there. The original is tagged correctly, I would've noticed the syntax highlighting otherwise. It's still square 1 for me.

Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/#findComment-1497300
Share on other sites

You are calling the chart() function within you index() function. When is that function being called?

 

Also make sure you include chart.php before calling your chart() function.

 

Also enable error reporting at the top of your script

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

// rest of your code
Edited by Ch0cu3r
Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/#findComment-1497310
Share on other sites

@chou3r: thanks for the suggestions, I did previously added include, and require as well, the latter just so it would trigger a fatal error if in fact something was wrong. Still got a blank page. Likewise when I incorporated your suggested error reporting. Thanks for the tip though. Puzzles me how such a spartan script can still fail.

Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/#findComment-1497360
Share on other sites

Finally, it's working! I had include 'chart.php', which wouldn't trigger an error warning. Thanks to @notioncommotion's suggestion, I used require instead, plus absolute, not relative path. Not sure why using absolute path would've made a difference, but it did. Thanks guys, this had been driving me crazy for a week.

Link to comment
https://forums.phpfreaks.com/topic/292633-cant-call-function/#findComment-1497459
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.