Wednesday 16 July 2014

Die() & Exit() Function's in PHP.

The Die() function in php is used for generally printing the message or we can say output.


For Example (i)-


<?php

$conn=mysql_connect("localhost","root","") or die("connection not found");

?>


Example (ii)-

<?php

           $x=5;
           $y=5.2;

if($x==$y)
{
           exit('both are equal');

}

else
{
           exit('both are not equal');
}

?>

The exit( ) function in php is alias of die( ) funciton. It is also used to printing the message.


For Example (i)-


<?php

$conn=mysql_connect("localhost","root","") or die("connection not found");

?>


Example (ii)-


<?php

$x=5;
$y=5.2;

if($x==$y)
{

exit('both are equal');

}

else
{

exit('both are not equal');
}

?>

Full list of Aliases function you will find on following URL:

http://php.net/manual/en/aliases.php


Posted By -Amit Tiwari

Sunday 13 July 2014

What is the difference between PhP4 & PhP5 ?

There are many differences between PHP4 and PHP5-

  1. PHP5 was powered by Zend Engine II,while PHP4 was powered by Zend Engine 1.0
  2. In PHP5 abstract classes are used but not used in PHP4.
  3. In PHP5 interfaces are used but not used in PHP4.
  4. In PHP5 visibility are used but not used in PHP4.
  5. In PHP5 magic methods (such as __call, __get, __set and __toString)are used but not uesd in PHP4.
  6. In PHP5 typehinting are used but not used in PHP4.
  7. In PHP5 incorporates static methods and properties.
  8. In PHP5 cloning are used but not used in PHP4.
  9. In PHP5 construtor are written as __construct keyword but in PHP4 are written as class name.
  10. In PHP5 special function intorduces called __autoload()
  11. In PHP5 there is new error level defined as ‘E_STRICT’
  12. In PHP5 there is new default extensions such as SimpleXML, DOM and XSL, PDO, and Hash.
  13. In PHP5 a new MySQL extension named MySQLi for developers using MySQL 4.1 and later.
  14. In PHP5, SQLite has been bundled with PHP.
Posted by Amit Tiwari

Monday 16 June 2014

How can we upload files in php.

If you want to upload any files in php.So first create the form by using <form></form> tag in php.

<html>
<body>
<form action="upload.php" method="post"
        enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>









Now,for uploading we have to create script for "upload.php" file.


<?php
if ($_FILES["file"]["error"] > 0) {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>


Using of PHP $_FILES array you can upload files from a client computer to the remote server.

The first parameter is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error". Like this:

$_FILES["file"]["name"]       // the name of the uploaded file.
$_FILES["file"]["type"]       // the type of the uploaded file.
$_FILES["file"]["size"]       //  the size in bytes of the uploaded file.
$_FILES["file"]["tmp_name"]   //the name of the temporary copy of the file stored on the server.
$_FILES["file"]["error"]      // the error code resulting from the file upload.

You can also put restrictions on Upload-

In this script we add some restrictions to the file upload. The user may upload .gif, .jpeg, and .png files; and the file size must be under 40 kB:

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 40000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
    echo "Error: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
} else {
  echo "Invalid file";
}
?>

Saving the Uploaded File-


<?php
             $allowedExts = array("gif", "jpeg", "jpg", "png");
             $temp = explode(".", $_FILES["file"]["name"]);
             $extension = end($temp);

if ((($_FILES["file"]["type"] == "image/gif")
            || ($_FILES["file"]["type"] == "image/jpeg")
            || ($_FILES["file"]["type"] == "image/jpg")
            || ($_FILES["file"]["type"] == "image/pjpeg")
            || ($_FILES["file"]["type"] == "image/x-png")
            || ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 40000)
&& in_array($extension, $allowedExts)) {
  if ($_FILES["file"]["error"] > 0) {
              echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
  } else {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
      echo $_FILES["file"]["name"] . " already exists. ";
    } else {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
    }
  }
} else {
  echo "Invalid file";
}
?>

The above script first checks if the file already exists, if it does not, it copies the file to a folder called "upload".


Posted by Amit Tiwari

Difference between include() & require()

Hello friend ..today i am going to teach you about the both { Include() & Require() } main functions in php.Both has same meaning but in a different manner.

Include() function

Syntax=include('main.php');

For this we have to create the file with extention of  ".php",

<?php
<body>
<a href="http://www.example.com/index.php">Home</a> <br />
<a href="http://www.example.com/about.php">About Us</a> <br />
<a href="http://www.example.com/services.php">Services</a> <br />
<a href="http://www.example.com/portfolio.php">Portfolio</a> <br />
<a href="http://www.example.com/Contact.php">Contact Us</a>
</body>
?>

Now ,save this file as main.php.for including this file we have to create new file called index.php. Here we know about the importance or we can say that how to use a include function,

<?php include("main.php"); ?>
<p>Hello this is my first php website</p>
</html>

Require() function-

Try the same example ,by using the function require() in the place of include().The only different is that require() function gives fatal error if there is no file found.

<?php include("nofile.php"); ?>
<p>Hello this is my first website</p>


Output=It gives fatal error and stop the script.








Posted by Amit Tiwari

Thankyou,

If you have any doubt related to post and want to know more about this so just comment in the below comment box,I ll response asap.


Friday 13 June 2014

PHP end() Function

Basically End function sets the pointer of an array to the last element of array.

Syntax-end(array)










For Example-



<?php
                $arr            =  array('name'=>'amit','age'=>'22','city'=>'bhopal','profession'=>'php developer');
                $lastValue   = end($arr);
?>

OUTPUT :  php developer

Posted Amit Tiwari

What is .htaccess?

.HTACCESS define as -

"It's a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn 'loaded via the Apache Web Server', then the .htaccess file is detected and executed by the Apache Web Server software."


Use of .htaccess-

1) .htaccess file is basically used for url rewriting.
2) .htaccess file used to make the site password protected.


Below example show how we can use the htaccess file.

.htaccess
















Posted by Amit Tiwari


Tuesday 10 June 2014

Difference between strstr and stristr in php.

Functions strstr() and stristr, these are used to find the first occurence of the string (case-sensitive) but stristr() is case insensitive.

strstr() - Find first occurrence of a string.

Syntax:
                   strstr ($string, string) 

Example:

<?php
                  $email = 'makersofweb@rAls.com';
                  $domain_name = strstr($email, 'A);
                 echo $domain_name;

?>

Output:

Als.com



stristr() - Find first occurrence of a string (Case-insensitive)




















stristr ($string, string) 

Example:

<?php
                     $email = 'makersofwb@rAls.com';
                     $domain_name = stristr($email, 'A');
                     echo $domain_name;
 ?>


Output-
              akersofwb@rAls.com

Posted by Amit Tiwari