Iain Simpson
Iain Simpson

Reputation: 469

Function Running When Not Called In PHP

Hi I am running the following script, the problem I am having is that the monthly functions are getting called even though it isn't the first Monday of the month ?. What is supposed to happen is every day the every day runs, every Monday the every Monday runs, every month the monthly runs.

By the way, I havnt called these script with cron on those dates as I have to ask the server owner to setup a cron job each time which is a major pain in the rear as he takes ages to do it, so its easier to just have cron hit the script every day and I control the rest with php.

// SETS THE TIMEZONE TO UK TIME
date_default_timezone_set('Europe/London');

// DEFINES WEEKDAY AND DAY OF THE MONTH
$weekday = date('D');
$dayOfMonth = date('d');

// RUNS THE EVERY DAY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY DAY
gasoildailyemailer();
dervdailyemailer();
kerodailyemailer();
if ($weekday == 'Mon')
{

// RUNS THE WEEKLY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY WEEK
 gasoilweeklyemailer();
 dervweeklyemailer();
 keroweeklyemailer();
if ($dayOfMonth <=6)

// RUNS THE MONTHLY FUNCTIONS TO MAIL USERS IN THAT GROUP EVERY MONTH
gasoilmonthlyemailer();
dervmonthlyemailer();
keromonthlyemailer();
}

?>

<?php

function gasoildailyemailer() {
echo 'GAS OIL DAILY';
};
function dervdailyemailer() {
echo 'DERV DAILY';
};
function kerodailyemailer() {
echo 'KERO DAILY';
};

?>

<?php

function gasoilweeklyemailer() {
echo 'GAS OIL WEEKLY';
};
function dervweeklyemailer() {
echo 'DERV WEEKLY';
};
function keroweeklyemailer() {
echo 'KERO WEEKLY';
};

?>

<?php

function gasoilmonthlyemailer() {
echo 'GAS OIL MONTHLY';
};
function dervmonthlyemailer() {
echo 'DERV MONTHLY';
};
function keromonthlyemailer() {
echo 'KERO MONTHLY';
};

?>

Upvotes: 1

Views: 1035

Answers (2)

Narf
Narf

Reputation: 14752

You're missing the brackets after if ($dayOfMonth <=6).

Upvotes: 1

Jon
Jon

Reputation: 437434

This is what you get for not always using braces:

if ($dayOfMonth <=6) 
    gasoilmonthlyemailer();  // THIS IS INSIDE THE IF
    dervmonthlyemailer();    // BUT THIS IS OUTSIDE!
    keromonthlyemailer();    // THIS ONE TOO!

Fix it by adding braces as you shoud always do (warning: possible religious argument here; this is just my own view):

if ($dayOfMonth <=6) {
    gasoilmonthlyemailer();
    dervmonthlyemailer();
    keromonthlyemailer();
}

As an aside: you would know that only two of the three functions were being incorrectly called. Why not spell that out explicitly so that everyone else benefits from that knowledge up front?

Upvotes: 1

Related Questions