Author Topic: Error on AND for checking 2 variables -SOLVED  (Read 1097 times)

0 Members and 1 Guest are viewing this topic.

Offline Phlerb

  • Expert Weather Dabbler
  • Member
  • *
  • Posts: 18
    • Old Pump Road Wx
Error on AND for checking 2 variables -SOLVED
« on: January 05, 2015, 09:16:26 AM »
Good day!

I am trying to create a script that checks what the current conditions are (DONE now via php call using API to wunderground) and then a second variable getting if it is night or day.

I am a bit shaky on this. I installed the sunrise-sunset.php file from Mike Challis, and then in my page I added

include 'sunrise-sunset.php';

which I hope would pull in the variable, one of which is $dayornight. So, I am hoping my code would do an if variable1 = true && variable2 = true  do this, elseif do something else.

But, I am getting an error on this:

Parse error: syntax error, unexpected 'and' (T_LOGICAL_AND) in /homepages/17/d554858752/htdocs/DEADCOWZdomainroot/wuapicall2.php on line 8

Here is some of the code on my page.
The top section is the API call (API changed for the forum), the second part is the include, then the logic part where it determines which image to copy and name as current.jpg. This DOES work without the && part, but not with it. Adding the AND breaks it.

Code: [Select]
<?php $json_string file_get_contents("http://api.wunderground.com/api/3aa3d2f44c50a999/geolookup/conditions/q/VT/Jericho.json"); 
$parsed_json json_decode($json_string); 
$weather $parsed_json->{'current_observation'}->{'weather'}; 

include 
'sunrise-sunset.php';


if (
in_array($weather, array('Mostly Cloudy''Cloudy''Partly Cloudy''Overcast'))) and ($dayornight =='night')
{
copy("/kunden/homepages/17/d554858752/htdocs/DEADCOWZdomainroot/images/current/nightcloudy.jpg""/kunden/homepages/17/d554858752/htdocs/DEADCOWZdomainroot/images/current/current.jpg");
}
elseif (
in_array($weather, array('Mostly Cloudy''Cloudy''Partly Cloudy''Overcast'))) and ($dayornight =='day')
{
copy("/kunden/homepages/17/d554858752/htdocs/DEADCOWZdomainroot/images/current/daycloudy.jpg""/kunden/homepages/17/d554858752/htdocs/DEADCOWZdomainroot/images/current/current.jpg");
}

I have searched google for this error and tried both && and 'and', removed the parentheses around the $dayornight =='night' but it always throws this error.

I set up a CRON job to run this page every 15 minutes to update the current.jpg

Any assistance would be appreciated!

Thank you!
Phil
« Last Edit: January 07, 2015, 02:23:50 PM by Phlerb »

Offline pfletch101

  • Forecaster
  • *****
  • Posts: 329
    • Personal Website
Re: Error on AND for checking 2 variables
« Reply #1 on: January 05, 2015, 09:50:25 AM »
You are missing one level of parentheses - should be:
if ((in_array($weather, array('Mostly Cloudy', 'Cloudy', 'Partly Cloudy', 'Overcast'))) and ($dayornight =='night'))
{...
The entire conditional statement must be in parentheses for the parser to interpret it correctly.
Vantage Pro 2+ connected to Raspberry Pi running weewx by means of Meteo-Pi - data incorporated in domestic energy production (PV) and use monitoring system.

Offline Murry Conarroe

  • Contributor
  • ***
  • Posts: 143
    • Wildwood Weather
Re: Error on AND for checking 2 variables
« Reply #2 on: January 05, 2015, 09:51:26 AM »
You need another set of parenthesis.
Change
Code: [Select]
if (in_array($weather, array('Mostly Cloudy', 'Cloudy', 'Partly Cloudy', 'Overcast'))) and ($dayornight =='night')to
Code: [Select]
if ((in_array($weather, array('Mostly Cloudy', 'Cloudy', 'Partly Cloudy', 'Overcast'))) and ($dayornight =='night'))
Murry

Offline Phlerb

  • Expert Weather Dabbler
  • Member
  • *
  • Posts: 18
    • Old Pump Road Wx
Re: Error on AND for checking 2 variables
« Reply #3 on: January 05, 2015, 10:12:17 AM »
Thanks Murry. When I added the ) via notepad++, there was no other corresponding ( to link to, and I got the same error 

 :-(

Phil

Offline pfletch101

  • Forecaster
  • *****
  • Posts: 329
    • Personal Website
Re: Error on AND for checking 2 variables
« Reply #4 on: January 05, 2015, 10:28:40 AM »
Thanks Murry. When I added the ) via notepad++, there was no other corresponding ( to link to, and I got the same error 

 :-(

Phil

Reread our replies carefully. You need an extra pair of parentheses.
Vantage Pro 2+ connected to Raspberry Pi running weewx by means of Meteo-Pi - data incorporated in domestic energy production (PV) and use monitoring system.

Offline wvdkuil

  • Wim van der kuil
  • Forecaster
  • *****
  • Posts: 1986
    • My PWS at Leuven Belgium Europe
Re: Error on AND for checking 2 variables
« Reply #5 on: January 05, 2015, 10:44:26 AM »
There must always be the exact same numer of  ( opening and ) closing, count them using +1 for ( and  -1 for ) end always end with zero.


Code: [Select]
if (  in_array($weather, array('Mostly Cloudy', 'Cloudy', 'Partly Cloudy', 'Overcast') )  and ($dayornight =='night') )
   1          2               3                                                      2 1      2                     1 0 
Hope this explains why you should add an ) at the end and remove one in the middle of the if.  If you are at zero the if should be ended.
« Last Edit: January 05, 2015, 10:46:29 AM by wvdkuil »

Offline Phlerb

  • Expert Weather Dabbler
  • Member
  • *
  • Posts: 18
    • Old Pump Road Wx
Re: Error on AND for checking 2 variables
« Reply #6 on: January 05, 2015, 10:49:28 AM »
Thank you everyone!

That was the problem! I thought I had checked and only saw one ( at the very beginning, but I guess I need new glasses. I now have the script live on my site and it is working like a charm! Every 15 minutes it checks WeatherUnderground for conditions, and updates the background image to correspond to that, depending upon day or night!

Thank you everyone!!!

 :grin: :grin: :grin: :grin:

Offline Ian.

  • Forecaster
  • *****
  • Posts: 460
    • Chatteris Weather
Re: Error on AND for checking 2 variables
« Reply #7 on: January 05, 2015, 11:38:18 AM »
I really like what you have done with your site, I have always felt mine and other sites looks quite bland with their plain backgrounds.
CWOP - DW3371
PWS - ICAMBRID16
https://www.chatteris.biz

Offline Phlerb

  • Expert Weather Dabbler
  • Member
  • *
  • Posts: 18
    • Old Pump Road Wx
Re: Error on AND for checking 2 variables
« Reply #8 on: January 05, 2015, 12:21:09 PM »
Thank you redpis, that means alot coming from you.

I plan on creating a package and readme on how to do it. I want to clean up the scripting a bit so that the full path is a variable so it looks a bit cleaner, as well as explain the cron job creation.

Once the coding is done I will host it (it may be a bit big as I would include my images) as a download and probably have Ken True review it also.

Take care!
Phil