The require() and require_once() functions perform same work. Both functions will include and evaluates the specific file while executing the code. if include() is not able to find a specified file on location at that time it will throw a warning however, it will not stop script execution. For the same scenerio require() will throw fatal error and it will stop the script execution. Example <?php echo “Hello”; ?> test.php <?php require(‘echo.php’); require_once(‘echo.php’); ?> test.php outputs: “Hello”.
PHP and HTML interact?
PHP is an HTML-embedded server-side scripting language. The goal of the language is to allow web developers to write dynamically generated pages quickly. NTC Hosting offers its clients high quality PHP and HTML hosting services. Our servers are configured so as to ensure maximum performance for both your HTML and PHP-based applications and the non-interruptible functioning of your websites. Example <?php $Fname = $_POST[“Fname”]; $Lname = $_POST[“Lname”]; ?> <html> <head> <title>Personal INFO</title> </head> <body> <form method=”post” action=”<?php echo $PHP_SELF;?>”> First Name:<input type=”text” size=”12″ maxlength=”12″ name=”Fname”><br /> Last Name:<input type=”text” size=”12″ maxlength=”36″ name=”Lname”><br /></form> <? echo “Hello, “.$Fname.” “.$Lname.”.<br />”; ?>
Multiple inheritance in PHP?
Inheritance is a well-established programming principle, and PHP makes use of this principle in its object model. This principle will affect the way many classes and objects relate to one another. For example, when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality. This is useful for defining and abstracting functionality, and permits the implementation of additional functionality in similar objects without the need to reimplement all of the shared functionality. Example <?php class Foo { public function printItem($string) { echo ‘Foo: ‘ . $string . PHP_EOL; } public function printPHP() { echo ‘PHP is great.’ . PHP_EOL; } } class Bar extends Foo { public function printItem($string) { echo ‘Bar: ‘ . $string . PHP_EOL; } } $foo = new Foo(); $bar = new Bar(); $foo->printItem(‘baz’); // Output: ‘Foo: baz’ $foo->printPHP(); // Output: ‘PHP is great’ $bar->printItem(‘baz’); // Output: ‘Bar: baz’ $bar->printPHP(); // Output: ‘PHP is great’ ?>
Laravel Training Institutes in Gurgaon
Laravel-5 Framework Training @ Webs Jyoti Laravel-5 Framework Training @ Webs Jyoti : Laravel is a free, open-source PHP web framework, created by Taylor Otwell and intended for the development of web applications following the model–view–controller (MVC) architectural pattern. Duration : 40 hrs for training related enquiry call us : 8802000175 Course Content Setup & Installation Basic Routing Basic Routing Route Parameters Responses Views Redirects Custom Responses Middleware Defining Middleware Registering Middleware Controllers Basic Controllers Controller Middleware Implicit Controllers Blade Templates Creating Templates PHP Output Control Structures Templates Template Inheritance Advance Routing Named Routes Secure Routes Parameter Constraints Route Prefixing Domain Routing URL Generation The Current URL Generating Framework URLS Asset URLs Generation Shortcuts Request Data Retrieval Old Input Upload Files Cookies Forms Forms Fields Buttons Security Basic Database Usage Configuration Read/Write Connections Running Queries Database Transaction Accessing Connections Query Builder Introduction Selects Join DML Queries Eloquent ORM Creating new models Reading Existing Models Updating Existing Models Deleting Existing Models Authentication Deploying and integrating third-party services into application
Data Sending Ways in PHP
There are some certain ways to send your information or data to the PHP page- Using Form Methods – Get & Post Using Cookies & Session Using Query String / URL Rewriting Using Hidden form fieldForm MethodsThe GET Method: The GET method sends information separated by the ? Operator.Example: http://www.xyz.com/home.html?city=Gurgaon Please note that the GET method is restricted to send upto 1024 characters (1024 bytes) only. You should not use GET method if you have password or other confidential information to be proceeding over the server. GET can’t be used to send binary data, like images or word documents, to the server. The PHP provides $_GET associative array or super global variable to access the data sent using GET method. The POST Method The POST method transfers information via HTTP headers. The POST method does not have any restriction on data size to be sent. So it can be used to send files, images as well as binary data as well. The PHP provides $_POST super global variable or associative array to access data sent using POST method. PHP Page: thanks.php <?php echo $_POST[‘email]; echo $_POST[‘pass]; ?> Here we have print the same value received through email and password field respectively. We can make it little advanced by taking above posted values to the variables. The advantage of using variable will be like we can use some validation and sanitizing data before preceding it to the database. So the above code can be modified something like- <?php // collect values in the variables separately $email= $_POST[‘email]; $password= $_POST[‘pass]; // you may use some validations like if($email==”” || $password==””) echo “Input required”; else echo “Welcome “.$email . “ Your password is “.$password; ?>
Error Handling in PHP
Error handling is the process of catching errors from the program and then taking appropriate action. Before proceeding for error handling you should get to know how many types of errors occurred in PHP. Notices: These are non-critical errors that occurred while executing a script – for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all – although, as you will see, you can change this default behavior. Warnings: These are more serious errors – for example, attempting to use a file (using include() method) which does not exist. By default, these errors are displayed to the user, but they do not result in script termination. Fatal Errors: These are critical errors – for example, creating an object of a non-existent class, or calling a function which doesn’t exist in your program. These errors cause the immediate termination of the script, and PHP‟s default behavior is to display them to the user when they take place. It’s very simple in PHP to handle errors. Using die() function While writing your PHP program you should check all possible error condition before going ahead and take appropriate action when required. Example <?php if(!file_exists(“/docs/resume.txt”)) { die(“File not found”); } else { $file = fopen(“/docs/resume.txt”,”r”); } ?> Some possible errors in PHP E_ERROR – Fatal Run-time errors. Execution of the script is halted E_WARNING – Non-fatal errors. Execution is not halted E_PARSE – Compile-time parse errors. E_NOTICE – Run-time notices. E_ALL – All errors and warnings, except level E_STRICT E_CORE_WARNING – Non-fatal run-time errors. This occurs during PHP’s initial start-up. There are some different locations where we can control errors: In the php.ini file In the .htaccess file on your web server From your own PHP code. We could do the same from our PHP code during runtime by calling the error_reporting() function as below: error_reporting(E_ALL); To turn on error logging and log the errors to our specific log file (instead of the default error log file, which is often the web server error log file) log_errors = on error_log = “/tmp/error.log”
PHP – Strings Functions
String functions are the most important functions frequently used in PHP. String is the sequence of characters, like “Let’s learn String functions”. There is no installation required to use string function. Example <?php $name = “Hirdesh Bhardwaj”; print($name); ?> String Concatenation Operator To concatenate two string variables together, we use the dot (.) operator − <?php $txt1=”Hello Readers”; $txt2=”8756″; echo $txt1 . ” ” . $txt2; ?> Using the strlen() function The strlen() function is used to get the length of a string. <?php $txt=”Webs Jyoti”; echo strlen($txt); ?> This will produce the following result – 10 (including space) Using the strpos() function The strpos() function is used to search for a text or character within a string. If the value is found then this will return the position of the first value. If no match is found, it will return FALSE. Let’s see if we can find the string “PHP” in our string − <?php echo strpos(“Hello PHP”,”PHP”); ?> This will produce the following result – 6 The explode() Function: Used to convert strings into an array variable. Example: $db = explode(‘;’,”host=localhost;db=hirdesh;uid=root “); print_r($settings); Output: Array ( [0] => host=localhost [1] => db=hirdesh [2] => uid=root [3] The substr() Function: The substr() is used to return part of the string. It accepts three (3) basic parameters. The first one is the string to be shortened, the second parameter is the position of the starting point, and the third parameter is the number of characters to be returned. The md5() Function: Used to calculate the md5 hash of a string value. This could be used where you want to convert your password from plain text to hash code to prevent the use of unauthorized users. The trim() function : the Trim() is used to remove white spaces and predefined characters from a both the sides of a string. Syntax trim(string,charlist) The str_replace() function: This can be used where you just want to swap out words or a set of characters in a string and replace them with something else.
OOPs Concepts – PHP
OOP is intimidating to a lot of developers because it introduces new syntax and, at a glance, appears to be far more complex than simple procedural, or inline, code. However, upon closer inspection, OOP is actually a very straightforward and ultimately simpler approach to programming. Before we go in details, let’s define important terms related to Object Oriented Programming. Class− Simply Class is a collection of member variables and its functions. Object−You define a class once and then make many objects that belong to it. Objects have state behavior and identity. Member functions and Variables− these are the functions and variables defined inside a class and are used to access object data. Inheritance– Simply creating a new class from the existing class. When a class is defined by inheriting existing function of a parent class then it is called inheritance. Polymorphism– Using this you can create multiple functions with their same name. Or simply saying function name will remain same but it makes take different number of arguments and can do different task. Overloading− a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation. Data Abstraction− Any representation of data in which the implementation details are hidden (abstracted). Encapsulation− refers to a concept where we encapsulate all the data and member functions together to form an object. Constructor– A Constructor is the same name of its class, will automatic invoke once the object of class in created. Destructor– It refer to a special type of function which will be called automatically whenever an object is deleted or goes out of scope. Example: <?php // creating class called myClass class myClass { public $i; // creating function called myFn function myFn() { $c=10; echo “Value is “.$c; } } // Creating object for the class myClass $ob=new myClass(); // Calling function through class object $ob $ob->myFn(); ?>
Working with Images – PHP
We need to create an HTML form that allows you to choose the file to be uploaded. Note: please make sure you have form method type is post and enctype=”mutipart/form-data” to to handle file uploading process successfully. HTML Code <form enctype=”multipart/form-data” action=”process.php” method=”POST”> Choose File : <input name=”myfile” type=”file” /> <input type=”submit” value=”Upload” /> </form> Now create a PHP file process.php for handling file information <?php // get the uploaded file name $filename= $_FILES[‘myfile’][‘name’]; // get the file type $filetype= $_FILES[‘myfile’][‘type’]; // get the file size (in bytes) $filesize= $_FILES[‘myfile’][‘size’]; // You may put your validations here i.e restrict file size if($filesize>=1000000) { echo “ File is too Large, Maximum file size should be 1 MB”; exit; } // Validation to check if uploaded file is an Image if($filetype !=”image/jpeg” && $filetype !=”image/png” && $filetype !=”image/gif” && $filetype !=”image/JPEG” $filetype !=”image/PNG”) { echo “ Only Images allowed , Kindly upload valid Image.”; exit; } else { // create a folder – upload (in the same location where you have this PHP file) to move uploaded image from your local computer to server $target = “upload/”; $target = $target . basename( $_FILES[‘myfile’][‘name’]) ; move_uploaded_file($_FILES[‘myfile’][‘tmp_name’], $target); echo ” Image Uploaded Successfully”; } ?> You will be saving your uploaded images to a folder on the web server, which means you need a directory that is writable by the web server. To make the folder writable by the web server, you can use your FTP client for the same. To write the uploaded image to the target folder, you use the function move_uploaded_file. This PHP function will retrieve the image and move it to the designated location.
Sending email – Mail Function in PHP
Almost in every web application sending automated email by system is the common requirement. So, it’s very common and important part that must be known by developer. The simple example, when a visitor to your website fills out a enquiry form. mail function return a boolen value, return true if email sent successfully, false otherwise Syntax: mail ($to , $subject , $message, $additional_headers); $to: This is the first parameter of mail function hold the email address of receiver, the address to which email has to be sent. $subject: Every email should have subject. This parameter cannot contain any newline characters. $message: This is the actual body or content of email to send. Please note that every line should be break by \n and every line should not exceed the limit of 70 characters. $addintional_header: This contain additional header values like From, CC or Bcc etc. This is the optional parameter in mail function. Example <?php $to = “info@websjyoti.com”; $subject = “Welcome to Webs Jyoti”; $txt = “Hi, You received a new enquiry from user. this is an automated mail”; $headers = “From: sales@websjyoti.com” . “\r\n” ; mail($to,$subject,$txt,$headers); echo “Mail sent successfully”; ?>