Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Friday, August 21, 2015

How to Create Slug

Create Slug
<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>

Friday, June 26, 2015

How to Create RSS Feed through PHP, MySQL

http://www.pontikis.net/blog/simple-rss-class-create-rss-feed

Sunday, December 14, 2014

Create Random Code with rand

<?php
$batch_code = 'DROID';
$characters = range('0','3');
for ($i = 0; $i < 4; $i++) {
$rand = mt_rand(0, count($characters)-1);
$batch_code .= $characters[$rand];
}
echo $batch_code
?>

Answer
DROID2130

***************************************

Create Random Number
<?php
echo "DU".substr(md5(uniqid(mt_rand(), true)), 0, 3);
?>

Answer
DU4e1

Saturday, December 13, 2014

My OOPS Concepts

Don't repeat the code its a main concept of OOPS language

four pillar of OOPS
  1. Abstraction
  2. Polymorphism
  3. Encapsulation
  4. Inheritance
Scope Specifiers are
Public -> Can be access from anywhere
Private -> Declared on Class level, can't be inherit
Protected -> Accessible only for the direct child

Constructor in PHP:-
  1. PHP use another constructor __Constuctor()
  2. There should be only 1 constructor in PHP
  3. Constructor is created by its own
  4. constructor is the first function of the object
Abstraction:-
  1. Make some ideal code and use that further
  2. To define rules and regulations
  3. Data hiding
  4. Essential data is hidden
  5. If we extend the parent class then we must redefine all the abstract methods of abstract parent class
e.g.
If we switch On button then Television is getting on its our primary focus, don't need to worry what is happen its behind.

Polymorphism:-
  1. One thing has many form
  2. Function Overloading is the example of this
e.g.
db.php is the example of this one function accepts the diff parameters

Encapsulation:-
  1. To give permission to meet code
  2. Assign permission to meet code
  3. Assign Permission to meet data
  4. Public, Private, Protected
Interface:-
  1. Interface are extra methods and properties which we need to assign to class
  2. Multiple is possible with the help of Interface.
  3. All the useful methods and variables are assigned inside the Interface
e.g.
Class Shaktiman extends human
implements flying
implements runable
implements growable

Class Arjun extends human
implements bhramashtra

Friday, December 12, 2014

Important Questions and Answers in PHP

Diff b/w Function and method?
All these function which called with the help of object are known as method
If one fn is in the class then its become method

How to make var safe?
{$amount}

Difference b/w print and printf?
printf has the capability to save formatted string for future use
e.g. sprintf("%d-%s-%d", 1, 'sep', 2014);

Why use $ in PHP?
to differentiate the variable, don't use datatype like c++, java

How to submit a form without using submit button?


Session vs Cookies?
  1. session is safer than cookies
  2. cookies save in user system while session file save in server
  3. Maximum cookie size is 4kb.
  4. Session is depends on cookie because it has session id
Diff b/w static and constant?
Static is execute only once in a file, constant each time used in the functions

PHP-MySQL vs Node.js-MangoDB
MangoDB -> Its require dedicated server
Node.js -> Its fast as compare to PHP because its not wait for the return.

MangoDB
  1. Save the file in .js form
  2. Its not use OOPS Concepts
  3. Here is no Normalization
  4. Its not use INSERT/UPDATE/DELETE Query
What's the use of PDO_MYSQL
  1. We can change the database at any point of time, its the main benefits of PDO_MYSQL
  2. Joomla and Zend using the PDO_MYSQL

Mail in PHP

Mail in PHP
update ssl in php.ini
gmail works on SSL(Socket Security Layer)

Update sendmail.ini file to send mail
Source:
http://stackoverflow.com/questions/1993070/sending-emails-with-wamp

Session and Cookies in PHP

Session & Cookie
session_start() -> will not override previous session
session_start() -> when its call at the same time blank file automatically created
session_destroy() -> To destroy session

Cookies
setcookies() -> Its use to destroy cookies

Flash Cookies
Write file at unknown file location, user will delete the cookies not that folder which located on user system.

Session vs Cookies?

  1. session is safer than cookies
  2. cookies save in user system while session file save in server
  3. Maximum cookie size is 4kb.
  4. Session is depends on cookie because it has session id

MySQL in PHP

Mysql Port
mysql_default_port -> 3306

There are two types of database
1. relational database (Normalization)
2. NoSQL(MangoDB) -> Here is query 100 times fast

Naming Conventions
1. Always use plural in table name
e.g. table name should be users not user
2. Always write command in uppercase
e.g. SELECT * FROM `users`
3. always have table name in lowercase
e.g. users
4. Use _ in b/w two words of table name
e.g. users_data

Type
DDL -> CREATE, DROP, ALTER, RENAME, TRUNCATE
DML -> INSERT, UPDATE, DELETE, SELECT
DCL -> REVOKE, GRANT
TCL -> BEGIN, SAVEPOINT, COMMIT, ROLLBACK

TRUNCATE -> Delete Complete Data from the table except the structure
CREATE -> To Create Table or Database
INSERT -> To Insert data in table
DROP -> To Drop particular Table or Database
UPDATE -> To Update data in table
DELETE -> To Delete Data from the table
ALTER -> To Change the structure of table
COMMIT ->
SELECT -> To Select the data from the Table
RENAME ->
ROLLBACK ->
REVOKE ->
BEGIN ->
SAVEPONIT ->

Useful Functions
mysql_connect
mysql_select_db
mysql_query
mysql_fetch_array
mysql_fetch_assoc
mysql_fetch_object
mysql_fetch_row

Note:-
In Mysqli we can connect with multiple database at the 

Regular Expressions in PHP

Regular Expression
Regex is used to match any pattern against any string
Its use for pattern matching
Its very powerful but use only in extreme case (Increase the lookup time)

wild card -> . $ * ^ + ? |
match scope -> () {} []

regex vs string/array functions ->
preg_match() ->
preg_replace() ->
preg_split() ->
preg_grep() ->

String in PHP

String Functions
str_replace ->
strpos ->
str_repeat ->
strrev ->
implode ->
strstr ->
strtolower ->
strtoupper ->
rand ->
unique ->
number_format ->
ciel ->
floor ->
abs ->
round ->
add_slashes ->
strip_slashes ->
strip_tags ->
strip ->
nl2br -> convert "\n" into html <br> tag
strlen -> Get the length of string, its retutn type is Int
ucwords -> Uppercase First Char of every word
ucfirst -> Uppercase first char of string

Array in PHP

Linear Array -> Array which have numeric key
Associative Array -> Keys are Alphanumeric
Multi Dimensional Array ->
array_shift -> return top element value
array_unshift -> return new size of array
array_pop -> it return value of last element
array_push -> return new size of array

Useful Array Functions
array -> string = implode
string -> array = explode
unset() -> remove that var
isset() -> check whether the element exist or not
array_merge() -> merge two arrays
array_combine() -> combine two array
array_reverse() -> reverse the value of array
array_shuffle() -> shuffle the values of array
array_filter() ->
array_key() ->
array_value() ->
array_unique() ->
array_map() ->
trim() ->
rtrim() ->
ltrim() ->
sort() ->
asort() -> sort all the values
ksort() -> sort array with key
krsort() ->
arsort() ->
rsort() ->
array_intersect() ->
array_shift() ->
array_slice -> Get value from any array b/w two index
array_key_exist ->

PHP Basics i Learn

Loosely coupled Language
No need to define datatype, PHP automatically recognized

Print
print, printf, sprintf, echo
to print array
print_r, var_export, var_dump

gettype
its returns the datatype of the var

echo vs print
both used to print var value but echo is faster than the print because print is also the function. (echo is not the function so its not wait for the return type)

Include Files in PHP
include 'filename.php'; //Include File for the entire file, if file not found give warning message
include_once 'filename.php'; //Include File only once, if file not found give warning message
require 'filename.php'; //Include File for the entire life cycle of file, if file not found give fatal error message
require_once 'filename.php'; //Include file only once, if file not found give fatal error message
Note:-
In PHP6 only use require_once

Super Global Variables
All the super global variables are associative arrays

$_REQUEST[] -> Use this if don't know form type is get or post
$_GET[] -> use to get value from the URL, GET less data max size is 2000 kb
$_POST[] -> Use to send value from the page to another page, Get more Data max size is 8MB
$_SESSION[] -> Its contain server information
$_COOKIES[] -> Its store in local system
$_ENV[] ->
HTTP_HOST ->
HTTP_USER_AGENT ->
HTTP_REFRRER ->
SCRIPT_FILENAME ->
PHP_SELF -> recognize theirself wto here to go
SERVER_PROTOCOL ->

Constant / OFFSET
Never change, first needs to define
define ("LOGIN", $con);

$_SERVER['REFFRER'] is used to make file secure, used to make Login secure

PHP Errors
E_NOTICE ->
E_WARNING -> Give Warning Message
E_STRICT -> Show the error message, needs to follow strictly
E_ALL ->
E_ERROR -> Fatal Error, Program will not execute
DEPRECATED -> Shows particular functions are outdated

Display Errors On and Off
ini_set('display_errors', 'off');
ini_set('display_errors', '1');
error_reporting(~E_NOTICE);
notice(E_NOTICE);

To Access Error on particular line use @

PHP.inp File Settings
upload_max_size
upload_max_file -> 20
Max_file_size
post_max_size
short_open_tag
memory_limit
Maximum Execution time -> 30sec
Session Time -> 24 minute

Mysql Port
mysql_default_port -> 3306

Magic Constant in PHP
__FILE__ -> show the file path e.g. http://localhost/jpc/index.php
__DIR__ -> show the directory path e.g. C:\\wamp\www\jpc\
__LINE__ -> Its show the line number
__FUNCTION__ ->
__METHOD__ ->
__NAMESPACE__ -> Its tell about the working directory

Encoding Techniques
one way hashing -> 
md5() ->
sha1() ->

two way hashing
base64_encode() ->
base64_decode() ->
url_encode() ->
url_decode() ->