0

i am trying to go over a code someone once did in our organization and now he is gone and no one can help me with that. we have an auto-deployment PHP scripts in the server to help us sync the server with github whenever a push is made (webhooks). it used to work fine but suddenly it stopped working... with my very little experience with this kind of procedures, i have found the PHP code in the server that should work when a push is made in github and basically it should do the whole procedure of auto-deployment. the problem is that i went over the code and i noticed something weird;

if ($payload->ref === 'refs/heads/development')
{   echo 'received updated in dev branch; bash/deploy_frontend2dev.sh ';
    exec('sh /var/www/html/bash/deploy_frontend2dev.sh', $output);
    var_dump($output);
}
else 
echo 'smth is called';

the "else" here is without any curly braces. i know there is a way to do an "if" statement without the curly braces but is it working with only one part of it without the braces?

also if you would like this is the code from the "exec":

cd /var/www/html/ezmob-frontend/;
#echo "/var/www/html/ezmob-frontend/";

git pull origin development;
echo "pulling file from dev branch";

rm -R ../dashboard;
echo "dashboard is updated";

unzip ezmob-frontend-built.zip -d ../;
#echo "dashboard is updated";

chmod 777 -R dashboard;

note that we didn't change nothing from when it worked but still suddenly it stopped auto-deploying...

thanks.

shay.k
  • 503
  • 1
  • 7
  • 15

3 Answers3

0

In general, yes, you can use the else without curly braces. But it is exactly to avoid the confusion you are experiencing now that you should consider always wrapping your ifs and elses in curly braces anyway.

It's a never ending discussion, so make your own decision. Here is why I think you should:

This is just why I think it's a good idea. But, to each his own. You should choose what you think is best.

hraban
  • 1,819
  • 1
  • 17
  • 27
0

IF ELSEIF and ELSE can be used without curly brackets like this.

if ($foo) :
    echo 'ok';
elseif ($bar) :
    echo 'ok';
else :
    echo 'nono';
endif;

If you're having code with only one line in the IF, ELSEIF or ELSE, then you can do it this way:

if ($foo)
    echo 'ok';
elseif ($bar)
    echo 'ok';
else
    echo 'nono';
Jesper
  • 3,816
  • 2
  • 16
  • 24
0

if and else accept a block of code, and that block of code can be a series of statements wrapped in {} or a single statement.

You can use

if
  one-statement-ending-in-semi-colon;

or

if {
   many-statements;
   each-ending-in-semi-colons;
}
user229044
  • 232,980
  • 40
  • 330
  • 338