• Resolved phpchick

    (@phpchick)


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

    Anyone have any suggestions? super simple

Viewing 9 replies - 1 through 9 (of 9 total)
  • What are you trying to do?

    Shouldn’t you add:

    $baboon = getstockstats(‘aapl’);

    echo $baboon;

    ?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

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

    Thread Starter phpchick

    (@phpchick)

    whats the difference between echo and return ?

    All I wanted to do was print “yourname is some guy”

    Thread Starter phpchick

    (@phpchick)

    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?

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

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

    Thread Starter phpchick

    (@phpchick)

    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

    Thread Starter phpchick

    (@phpchick)

    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

    Thread Starter phpchick

    (@phpchick)

    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

    Thread Starter phpchick

    (@phpchick)

    Solved!

    I removed the quotes.

    $objYahooStock->addStock($stocksymbol);

    Thank you everybody for so much help ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Extremely simple function not working, help?’ is closed to new replies.