The main difference between echo() and print() is that echo is just an
statement not a function and doesn't return's value or it just prints a
value whereas print() is an function which prints a value and also it
returns value.
Tuesday, October 4, 2011
Yii
Yii is a high-performance PHP framework best for developing Web applications. Yii comes with features: MVC, DAO/ActiveRecord, I18N/L10N, caching, authentication and role-based access control, scaffolding, testing, etc. It can reduce your development time significantly.
Select Top Second Salary
1. To find exact second salary
select max(salary) from jos_emp where salary < (select max(salary) from jos_emp)
2. To find all second salary
select salary from jos_emp where salary=(select max(salary) from jos_emp where salary < (select max(salary) from jos_emp)).
select max(salary) from jos_emp where salary < (select max(salary) from jos_emp)
2. To find all second salary
select salary from jos_emp where salary=(select max(salary) from jos_emp where salary < (select max(salary) from jos_emp)).
Monday, October 3, 2011
html_entity_decode() & htmlentities()
htmlentities() — Convert all applicable characters to HTML entities
html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities to their applicable characters from string.
html_entity_decode — Convert all HTML entities to their applicable characters
html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities to their applicable characters from string.
html_entity_decode — Convert all HTML entities to their applicable characters
How to find number of arguments passed in a function in php?
func_num_args — Returns the number of arguments passed to the function
example:
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3);
?>
example:
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3);
?>
Session and Cokkies
COOKIE
A cookie is a text-only string that takes a place in the memory of user’s browser.
If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.
SESSION
Session values are store in server side not in user’s machine.
A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.
The Differences
1. The key difference would be cookies are stored in client side and sessions are stored in server side.
2. The second difference would be cookies can only store strings. We can store our objects in sessions. Storing objects in sessions were really useful according to my experience.
3. Another difference was that we could be save cookie for future reference, but session couldn’t.
When users close their browser, they also lost the session.
A cookie is a text-only string that takes a place in the memory of user’s browser.
If the lifetime of the cookie is set to be longer than the time user spends at that site, then this string is saved to file for future reference. User could be disabled the cookie in their browser setting.
SESSION
Session values are store in server side not in user’s machine.
A session is available as long as the browser is opened. User couldn’t be disabled the session. We could store not only strings but also objects in session.
The Differences
1. The key difference would be cookies are stored in client side and sessions are stored in server side.
2. The second difference would be cookies can only store strings. We can store our objects in sessions. Storing objects in sessions were really useful according to my experience.
3. Another difference was that we could be save cookie for future reference, but session couldn’t.
When users close their browser, they also lost the session.
Difference between fputs and fwrite
1. The fputs() writes to an open file.
2. The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
3. This function returns the number of bytes written on success, or FALSE on failure.
4. The fputs() function is an alias of the fwrite() function.
5. There is no difference between fputs and fwrite, fputs() function is an alias of the fwrite() function.
2. The function will stop at the end of the file or when it reaches the specified length, whichever comes first.
3. This function returns the number of bytes written on success, or FALSE on failure.
4. The fputs() function is an alias of the fwrite() function.
5. There is no difference between fputs and fwrite, fputs() function is an alias of the fwrite() function.
Difference between include and require
1. The two functions are used to insert the content of a file into another PHP file before it is executed by the server.
2. They are identical in every aspect, but they perform error handling in different ways.
3. The include() function generates a warning (which does not halt execution) while the require() function generates a fatal error (which stops execution immediately).
2. They are identical in every aspect, but they perform error handling in different ways.
3. The include() function generates a warning (which does not halt execution) while the require() function generates a fatal error (which stops execution immediately).
Error display functions
1.ini_set('display_errors', 1);
2.ini_set('log_errors', 1);
3.ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
4.error_reporting(E_ALL);
2.ini_set('log_errors', 1);
3.ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
4.error_reporting(E_ALL);
Method Overriding
Method overriding is when the function of base class is re-defined with the same name, function signature and access specifier (either public or protected) of the derived class.
OOPs concept in php
1.Inheritance
Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.
Php support multiple inheritance by using interface.
2.Abstract class
It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility
An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement.The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract. Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created
3.Interface
An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour. To extend from an Interface, keyword implements is used.
Diff btw abstract & interface
For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation
For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
All methods declared in an interface must be public.
Abstract class can contain variables and concrete methods.
Interfaces cannot contain variables and concrete methods except constants.
A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
A class can implement many interfaces and Multiple interface inheritance is possible.
Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.
Php support multiple inheritance by using interface.
2.Abstract class
It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility
An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement.The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract. Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created
3.Interface
An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour. To extend from an Interface, keyword implements is used.
Diff btw abstract & interface
For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation
For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
All methods declared in an interface must be public.
Abstract class can contain variables and concrete methods.
Interfaces cannot contain variables and concrete methods except constants.
A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
A class can implement many interfaces and Multiple interface inheritance is possible.
Magic methods
1. __toString() Magic method
The __toString() method is automatically called when an object is converted into a string for the purpose of display or concatenation
2. __get() and __set() Magic Method
__get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class
3. __isset() and __unset() Magic Method
The magic method __isset() method receives an argument - the value of which is the name of the variable that the program wants to test if the variable is set or not. The magic method __unset() method receives an argument - the value of which is the name of the variable that the program wants to unset.
4. __call() Magic method
These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development
5. __autoload() method
The magic method __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them The magic method __autoload() is not included in your class definition as this is to be called once in a script. The best place to put the autoload() file is in your configuration file which is loaded in all your other scripts
6. __sleep() and __wakeup()
The magic method __sleep() and __wakeup() is called when an object is serialized. The magic method __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized.
__sleep() magic method is called when the object of a class is about to be serialized. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don’t wish to serialize a particular class member, you should not include it in the array.
__wakeup() magic method is the opposite of the __sleep() method. It is called when the object of a class is about to be unserialized. This magic method __wakeup() does not accept any parameter nor returns anything. The __wakeup() method is responsible for setup operations after an object has been unserialized
7. __clone() method
to create a new $obj2 object we must clone an object to create a new one. To clone an PHP5 Object a special keyword i.e. clone is used
The __toString() method is automatically called when an object is converted into a string for the purpose of display or concatenation
2. __get() and __set() Magic Method
__get() is used when value from an undefined variable is to be read and __set() is used when a value is to be assigned to a undefined variable of a class
3. __isset() and __unset() Magic Method
The magic method __isset() method receives an argument - the value of which is the name of the variable that the program wants to test if the variable is set or not. The magic method __unset() method receives an argument - the value of which is the name of the variable that the program wants to unset.
4. __call() Magic method
These methods are automatically called internally when the program tires to execute a method that has not been defined within the class at the time of development
5. __autoload() method
The magic method __autoload() function is a convenience that allows you to use classes without having to explicitly write code to include them The magic method __autoload() is not included in your class definition as this is to be called once in a script. The best place to put the autoload() file is in your configuration file which is loaded in all your other scripts
6. __sleep() and __wakeup()
The magic method __sleep() and __wakeup() is called when an object is serialized. The magic method __sleep() and __wakeup() provides a method to clean up and restore objects before being serialized.
__sleep() magic method is called when the object of a class is about to be serialized. This magic method __sleep() does not accept any parameter and returns an array. The array should contain a list of class members that should be serialized. This means that if you don’t wish to serialize a particular class member, you should not include it in the array.
__wakeup() magic method is the opposite of the __sleep() method. It is called when the object of a class is about to be unserialized. This magic method __wakeup() does not accept any parameter nor returns anything. The __wakeup() method is responsible for setup operations after an object has been unserialized
7. __clone() method
to create a new $obj2 object we must clone an object to create a new one. To clone an PHP5 Object a special keyword i.e. clone is used
Magic Quotes
About magicquotes
1.' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically, This is identical to what addslashes() does.
2.to help prevent SQL Injection
3.on or off value for magic qoutes changed in php.ini file
1.' (single-quote), " (double quote), \ (backslash) and NULL characters are escaped with a backslash automatically, This is identical to what addslashes() does.
2.to help prevent SQL Injection
3.on or off value for magic qoutes changed in php.ini file
Array Functions
Array functions with syntax
1.in_array(search,array,type)
The in_array() function searches an array for a specific value, This function returns TRUE if the value is found in the array, or FALSE otherwise
2.count(),sizeof() both are same
3.list(var1,var2...)
The list() function is used to assign values to a list of variables in one operation, This function only works on numerical arrays
4.array_sum()
Returns the sum of the values in an array
5.array_unique(array1)
Removes duplicate values from an array
6.array_combine(array1,array2)
The array_combine() function creates an array by combining two other arrays, where the first array is the keys, and the other array is the values.
6.sort()-Sorts an array
7.rsort()-Sorts an array in reverse order
1.in_array(search,array,type)
The in_array() function searches an array for a specific value, This function returns TRUE if the value is found in the array, or FALSE otherwise
2.count(),sizeof() both are same
3.list(var1,var2...)
The list() function is used to assign values to a list of variables in one operation, This function only works on numerical arrays
4.array_sum()
Returns the sum of the values in an array
5.array_unique(array1)
Removes duplicate values from an array
6.array_combine(array1,array2)
The array_combine() function creates an array by combining two other arrays, where the first array is the keys, and the other array is the values.
6.sort()-Sorts an array
7.rsort()-Sorts an array in reverse order
String functions
String functions with syntax
1.addcslashes(string,characters)
The addcslashes() function returns a string with backslashes in front of the specified characters.
2.addslashes(string)
The addslashes() function returns a string with backslashes in front of predefined characters,This is safe in a database query
3.substr(string,start,length)
The substr() function returns a part of a string
4.ucfirst()
Converts the first character of a string to uppercase
5.ucwords()
Converts the first character of each word in a string to uppercase
6.strtolower(),strtoupper() etc..
7.strstr(strin,search)
The strstr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
8.stristr(strin,search)
Case-insensitive strstr
1.addcslashes(string,characters)
The addcslashes() function returns a string with backslashes in front of the specified characters.
2.addslashes(string)
The addslashes() function returns a string with backslashes in front of predefined characters,This is safe in a database query
3.substr(string,start,length)
The substr() function returns a part of a string
4.ucfirst()
Converts the first character of a string to uppercase
5.ucwords()
Converts the first character of each word in a string to uppercase
6.strtolower(),strtoupper() etc..
7.strstr(strin,search)
The strstr() function searches for the first occurrence of a string inside another string.This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
8.stristr(strin,search)
Case-insensitive strstr
Subscribe to:
Posts (Atom)