if then statement equivalent in php

3.1k views Asked by At

I am newish to php. But when i learned to program it was on a TI-83 Calculator. with the TI83 their was an if-then statements the could be used. I am writing some code to check if a directory is already created and if not create it. I need it to run the last half of the code either way. here is the code.

<?php
$dir = "/Applications/XAMPP/xamppfiles/htdocs/upload/$_POST[Itemid]/" ;
if (!file_exists($dir) and !is_dir($dir)) {
    $upload_dir = mkdir($dir, 0777);
}else{
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
    $filename = $_FILES["file"]["name"][$key];
    $filetemp = $_FILES["file"]["tmp_name"][$key];
$filetype = $_FILES["file"]["type"][$key];
$filesize = $_FILES["file"]["size"][$key];
   if (file_exists($dir . $filename))
  {
  echo $filename . " already exists. <br><br>";
  }
else
  {
  echo "Upload: " . $filename . "<br>";
  echo "Type: " . $filetype . "<br>";
  echo "Size: " . ($filesize / 1024) . " kB<br>";
  echo "Temporarily Stored in: " . $filetemp . "<br>";
  move_uploaded_file($filetemp,"$dir/$filename");
  echo "uploaded the file \"" . $filename. "\" to the \"/Applications/XAMPP/xamppfiles/htdocs/upload/\" Directory<br>" ;
  $con=mysqli_connect("localhost","root","","Inventory");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$sql="INSERT INTO ListPics (`1`, `Item_ItemID`)
VALUES
('$dir$filename', '$_POST[Itemid]')";


if (!mysqli_query($con,$sql))
 {
die('Error: ' . mysqli_error($con));
}
echo ($filename. " added to Database<br><br>");

mysqli_close($con);
  }
  }
  }
  }
?>

Yes I know this code is not secure. It will not ever see the interweb(Reverently bows head and says "Thank you Al Gore") It will sit snugly behind a VPN for personal use only. I just need to know how to execute the PHP equivalent of If Then Statements

1

There are 1 answers

0
elixenide On BEST ANSWER

This is simple:

if(condition) {
    /* do something if condition is true */
}
else {
    /* do something else if condition is false */
}
/* do other stuff either way */

You only need the else if there is something that you want to do only if the condition is false. If you want to do it either way, then don't use else at all.