WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Extremely simple function not working, help? (10 posts)

  1. phpchick
    Member
    Posted 1 year ago #

    function getstockstats($stocksymbol)
      {
     if ( $stocksymbol == "aapl" ) {
    	$result = "Your name is someguy!<br />";
    }
    return $result;
      }
    
    getstockstats('aapl');

    Anyone have any suggestions? super simple

  2. Taart
    Member
    Posted 1 year ago #

    What are you trying to do?

    Shouldn't you add:

    $baboon = getstockstats('aapl');

    echo $baboon;

    ?

  3. Andrew
    Forum Moderator
    Posted 1 year ago #

    What isn't working, in which line?
    What, if any, are the errors shown?

  4. phpchick
    Member
    Posted 1 year ago #

    whats the difference between echo and return ?

    All I wanted to do was print "yourname is some guy"

  5. phpchick
    Member
    Posted 1 year ago #

    I changed to this,

    function getstockstats($stocksymbol)
      {
     if ( $stocksymbol == "aapl" ) {
    	$result = "Test!<br />";
    }
    echo $result;
      }

    and it worked. But shouldnt returning $result also print it out?

  6. alchymyth
    Forum Moderator
    Posted 1 year ago #

    http://php.net/manual/en/function.echo.php
    http://php.net/manual/en/function.return.php

    however simple functions you are trying to code, you need to learn the basics.

  7. phpchick
    Member
    Posted 1 year ago #

    One last question,

    function widget($args, $instance)
      {
    
      function getstockstats($stocksymbol)
      {
    	include_once('class.yahoostock.php');
    	$objYahooStock = new YahooStock;
    	$objYahooStock->addFormat("snl1d1t1cvc1p2");
    	$objYahooStock->addStock("msft");
    
    	foreach( $objYahooStock->getQuotes() as $code => $stock)
    {
        ?>
        Code: <?php echo $stock[0]; ?> <br />
        Name: <?php echo $stock[1]; ?> <br />
        Last Trade Price: <?php echo $stock[2]; ?> <br />
        Last Trade Date: <?php echo $stock[3]; ?> <br />
        Last Trade Time: <?php echo $stock[4]; ?> <br />
        Change and Percent Change: <?php echo $stock[5]; ?> <br />
        Volume: <?php echo $stock[6]; ?> <br /><br />
        <?php
    }
    
     if ( $stocksymbol == "aapl" ) {
    	$result = "Test!<br />";
    	echo $result;
    }
      }
    
    getstockstats("aapl");
        echo $after_widget;
      }

    Why doesn't my function work when I add the echos inside that foreach loop? I have a feeling it is some sort of permissions issue but am not sure

  8. phpchick
    Member
    Posted 1 year ago #

    UGH.

    The directory starts in the plugin root and NOT the directory that the file is located in.

    I changed
    include_once('class.yahoostock.php');
    to
    include_once('prediction-widget/class.yahoostock.php');

    and now it works. ARGH

  9. phpchick
    Member
    Posted 1 year ago #

    One more issue

    $objYahooStock->addStock("$stocksymbol");

    That line is getting read as $stocksymbol
    What do I have to do change in order to make it read the variable, as opposed to $stocksymbol?

    I've tried wrapping in braces like

    $objYahooStock->addStock("{$stocksymbol}");

    but it did not work

  10. phpchick
    Member
    Posted 1 year ago #

    Solved!

    I removed the quotes.

    $objYahooStock->addStock($stocksymbol);

    Thank you everybody for so much help :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.