Skip to main content

Posts

Showing posts with the label PHP

Type Juggling and Type Casting

Type Juggling: PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

Uploading XML file into PHP

If you want to upload XML file into PHP, this tutorial will teach you first to make XML file and then mechanism to upload into your database using PHP. To Download full files, Click Here   First start with creating XML documents

Basic PHP Constructs

A construct is just short for control structure. Unlike an expression the construct creates a flow of control in the code. The construct controls how or when statements are executed, in order to create a structure of code that effectively tackles the task the programmer aims to achieve. Think of constructs like a place where code gets directed (or literally constructed) during execution. Imagine a busy intersection where lots of cars need to pass through. A construct is like a traffic light that directs how the traffic flows so that there isn’t an accident.

Encapsulation in PHP

Encapsulation is wrapping some data in an object. It is about controlling the visibility of things and define something like the order of layers of our application. Encapsulation in PHP OOP is about control and limit which parts of our application can access some class methods and properties using three keywords called in OOP world Access Control Modifiers , and these keywords are : public , private , protected   and this keyword comes in the beginning of declaring method or property just like below : class Member {   public $id;   protected $username;   private $password; } Public keyword modifier is used to define a variable or function as being visible to everyone, everywhere.  When this modifier is used the variable or function following is visible to the users using your class as well as all the inner workings of your class.  This modifier is the only modifier that allows interaction outside of the class. Private keyword modifier is use...

Sending mail from localhost

Okay now you want to test the mail function but wait you don't have any domain name or online site to do this. So what can you do in this situation. Simply send email from your localhost. Ya!! That's correct sending mail from your local computer and local files which are not hosted yet. And yes you can do this. For this follow the steps below: 1) First open php.ini file. 2) Search for the mail function. When you find something like below. Okay you have found it. 3) Now configure that mail function as below: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = smtp.wlink.com.np ; http://php.net/smtp-port smtp_port = 25 4) Okay this is good now next step. Search for the below line and configure the email address that you want to sent other emails. ; For Win32 only. ; http://php.net/sendmail-from ;sendmail_from = you@yourdomain 5) Remove semicolon(;) and write your email address as below: ; For Win32 only. ; http://php.net/sendmail-from sendmail_from = hello@gmail.com 6...

Constructors and Destructors

Constructor PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. Syntax: void __construct ([ mixed $args [, $... ]] ) For example: <?php class constExample{     function __construct()     {         echo "This will execute without calling a method";     }     } $obj = new constExample(); ?> Destructors PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence. Syntax: void __destruct ( void ) For example: <?php class MyDestructableClass {    function __construct() {      ...

Class and object

Creating a Class: To create a class use the class keyword and a name. Class names can be any combination of numbers and letters, although they must not begin with a number. The code associated with a class must be enclosed within braces. Class Defination: <?php class ClassName {   // class body } ?> For example: <?php class Books{ public $name; public $author; public function showBook(){ $name="Munna Madan"; echo $name; } } ?> Setting Properties and Methods In the class body are defined its properties and methods.         - Properties are variables defined inside the class.         - Methods are functions created within the class, with the word "function". The name of the variable (property) and the "function" word must be preceded by a specific attribute that determines the level of access that is allowed for the property or the method to be accessed outside its class. This can be: public, protec...

Object Oriented Programming

PHP is an object- oriented programming language. What is object-oriented programming (OOP)? Object-oriented programming(OOP) is a programming language using objects usually instances of a class, consisting of data fields and methods together with their interactions to design applications and computer programs. Main features of OOP are as below:  Class and object:     A class is a group of objects and an object is an instance of a class. It contains the code for properties and methods. Encapsulation:     Encapsulation is the ability of an object to protect the data (properties) of a class from outside influences. You can use a class (its properties) through an object instance, without having access to its code. Inheritance:     Inheritance is the ability of a class to copy or inherit the properties and methods of a parent class. Polymorphism:     Polymorphism is a class ability to do more different things, or use one clas...

MySQL: SQL

SQL is a standard language for accessing and manipulating databases. To connect any and every database and to execute, insert, update or delete any records, we need SQL.. Brief description about SQL:     SQL stands for Structured Query Language     SQL lets you access and manipulate databases     SQL is an ANSI (American National Standards Institute) standard What Can SQL do?     SQL can execute queries against a database     SQL can retrieve data from a database     SQL can insert records in a database     SQL can update records in a database     SQL can delete records from a database     SQL can create new databases     SQL can create new tables in a database     SQL can create stored procedures in a database     SQL can create views in a database     SQL can set permissi...

MySQL Connection

Connection with MySQL Database Before accessing database, you must create a connection to the database Syntax: mysql_connect(servername,username,password); where, servername specifies the server to connect to. Default value is “localhost” username specifies the username to log in with. Default value is the name of the user that owns the server process. To connect offline we use username “root”. password specifies the password to log in with. Default is “” Code : Creating connection to the database and selecting the required database <?php $con = mysql_connect(“localhost”,”root”,”"); if (!$con) { die(‘Could not connect: ‘ . mysql_error()); } else{ mysql_select_db(“test”, $con) }; ?> Here, we have store connection in a variable called $con and trap error using die function. Closing connection The connection will be closed automatically when the script ends. To close the connection before, use the mysql_close() function: <?php $con = mysql_conne...

MySQL: Introduction

MySOL is a database. With PHP, default database is MySQL. Database is needed to create any dynamic website or web application. Records or information for any website is kept in database in the forms of tables. Table is the collection of related data entries and it consist of columns and rows. A row of information is called tuple. What is Query? Query is a question or a request. With the help of query you can show, add,edit and delete any record. How to create database? Its very simple, just open your database, create database as below: 1)Open link http://localhost/phpmyadmin. 2)Click on the database tab. 3)Give name of database and create like below.   now  I will inside the database called test as below: How to create tables? 1)Once you have created database, click on the database. For example, I have created a database called test. So  2) Create table on the database test. 3) Give table name and number of fields needed and click on c...

PHP Error Handling

All the possible errors should be managed in such a way, it should be debugged properly. So in this case default error handling is used which is very simple. An error message with filename, line number and a message describing the error is sent to the browser. Error handling is an important part while creating any web applications or scripts or pages. If you want your viewers to not display any errors on your web page then you following code on the top of your page:    error_reporting(0); or if you only want to see Warning Messages and not Notice Messages: ini_set('display_errors',1); error_reporting(E_ALL); Error Handling with die() function Lets try using a simple example below: Code: <?php if(isset($_POST['submit'])){ $filename=$_FILES['image']['name']; $tmp_name=$_FILES['image']['tmp_name']; if(!move_uploaded_file($tmp_name,"img".$filename)){ die("Error in uploading"); } else { print("Up...

Sending Emails

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient’s email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters ); Here is the description for each parameters. Parameter Description to Required. Specifies the receiver / receivers of the email subject Required. Specifies the subject of the email. This parameter cannot contain any newline characters message Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters headers Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n) parameters Optional. Specifies an additional parameter to the sendmail program As soon as the mail function is called PHP will attempt to send the email then...

PHP Sessions

A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit. A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration. Starting a PHP Session: A PHP session is easily started by making a call to the session_start() function.This function first checks if a session is already started and if none is started then it starts one. It is recommended to put the call to session_start() at the beginning of the page. Session variables are stored in associative array called $_SESSION[]. These variables can be accessed during lifetime of a...

PHP Cookies

A cookie is often used to identify a user.A cookie is a tiny little file on your client’s hard drive which contains data you have asked to be stored. Some clients specifically configure their browser to reject cookies, believing for one reason or another that they are malicious, and there is nothing you can do about this – that person’s browser will not be able to store your data. When creating cookies, you specify how long you want it to be valid for, and, once done, the cookie remains in place until that date, when it “expires”. Cookies are automatically sent to the web server (and received/parsed by PHP) each time a user visits you. That means that once we place our cookie, our visitors’ browsers will automatically send the contents of that cookie across to us each time they view our messageboard index, and PHP will read the value into the $_COOKIE superglobal array. As cookies are sent each time, it is incredibly important not to store too much information there – they can real...