Author Topic: Calling advforecast2.php twice  (Read 790 times)

0 Members and 1 Guest are viewing this topic.

Offline UpstateWeather.com

  • Signature
  • Contributor
  • ***
  • Posts: 146
    • Upstate Weather
Calling advforecast2.php twice
« on: November 01, 2017, 04:50:09 PM »
I want to put today's and tomorrow' forecast icon and temp on the menubar.php file on the left hand side of my page. However, if I call advforecast2.php on the menubar.php file, then I can't display any of the forecast icons, text etc. on the main body of the website. If I require or include advforecast2.php again, nothing happens.

I'm sure there is a simple explanation, but I don't know what it is. Can anyone help?

http://www.ericboettner.com/weather

Thanks!
Eric

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9244
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
Re: Calling advforecast2.php twice
« Reply #1 on: November 01, 2017, 06:31:20 PM »
Actually, if you try to include() or require() any of the support scripts (like advforecast2.php) a second time on a page, it will result in a PHP fatal error because functions cannot be redefined. 

The way this all works is:
1) the primary page (index.php for example) starts the process of looking for PHP markup, and doing all the gathering of include() or require() scripts needed, along with any include() or require() that those need (can be continued to unlimited levels, limited only by the PHP process memory available).
2) as each script is loaded, it is parsed into 'metacode' and all functions defined in the scripts are enumerated -- so if a subsequent script tries to define a function of a name that is already used (such as include() the same script twice), the entire PHP process fails with a fatal error.
3) when the base .php page has had all the include()/require() parts successfully loaded, it executes the 'metacode' it has parsed, starting with the first <?php ... ?> set, replaces the contents of the HTML page with any print/echo statements, and proceeds to follow the execution path until complete, then
4) the webserver returns the fully generated page to the browser.

As a consequence of this 'can't redefine a function' issue, always use include_once() and require_once() instead of include() and require() since the former will only do the inclusion if it hasn't already been done.

Be aware that the [wx]index.php page and wxforecast.php page will do an include_once() for advforecast2.php, but not other pages on the (stock Saratoga) site.   Just add a
Code: [Select]
<?php
include_once("advforecast2.php"); ?>
to any page you need to have the $forecast... variables available for print/echo use.

Hope this helps...

Best regards,
Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline UpstateWeather.com

  • Signature
  • Contributor
  • ***
  • Posts: 146
    • Upstate Weather
Re: Calling advforecast2.php twice
« Reply #2 on: November 02, 2017, 09:17:10 AM »
As always Ken, THANK YOU! Your knowledge is amazing!

 

anything