Saturday, June 6, 2009

Lower Case an Array

Lower Case an Array

Here is an example of how to force all values in array tolower case. This could, of course, be used in the same way to make the array UPPERCASE or do anything else you wish to the array values. Note the that the string is passed to the array by reference. This is so the actual array values are manipulated.

<?php

/*** an array of mixed case words ***/
$array = array('MAC', 'Apple', 'osX');

/**
*
* @Lower case an array
*
* @param ref string $string
*
* @return string
*
*/
function lower(&$string){
$string = strtolower($string);
}

/*** apply the lower function to the array ***/
array_walk($array,'lower');

foreach($array as $value){
echo $value;
}
?>

Wednesday, April 29, 2009

PHP SCRIPT-- TO TIME OUT THE SESSION AFTER THE PARTICULAR TIME [SESSION TIME OUT]

<?php
if (!isset($_SESSION['timeout_idle'])) {
$_SESSION['timeout_idle'] = time() + MAX_IDLE_TIME; //set time at the first time of writing script
} else {
if ($_SESSION['timeout_idle'] < time()) {
//destroy session if session time is less the what we have set the time
} else {
$_SESSION['timeout_idle'] = time() + MAX_IDLE_TIME; // if continue working on similar site then increase the time of script
}
}
?>

PHP- Remove Special Character during website

// put this code on the top of the page or in your config file. You will never see the special character on that page/website(if you used config on the top then it work on whole website)


header("Content-type: text/html; charset=UTF-8");
ini_set('mbstring.internal_encoding', 'UTF-8');

Tuesday, April 28, 2009

SEO CONCEPT--GET META TAG IN ARRAY-- SUPPORTED BY PHP

<?php
// Assuming the above tags are at www.example.com
$tags = get_meta_tags('http://www.example.com/');

// Notice how the keys are all lowercase now, and
// how . was replaced by _ in the key.
echo $tags['author']; // name
echo $tags['keywords']; // documentation
echo $tags['description']; // discription
echo $tags['geo_position']; // 49.33;-86.59

var_dump($tags); // get all values

?>

Friday, April 24, 2009

Secure file download from PHP server

< ?php
// local file that should be send to the client
$local_file = 'filename';
// filename that the user gets as default
$download_file = 'if_u_wanna_to_change_file_name_i._e._uploade_then name';
// I u do not want to change name then wrtre
//$download_file = 'if_u_wanna_to_change_file_name_i._e._uploade_then name';


// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: filename='.$download_file);

// flush content
flush();
// open file stream
$file = fopen($local_file, "r");
while(!feof($file)) {

// send the current file part to the browser
print fread($file, round($download_rate * 1024));

// flush the content to the browser
flush();

// sleep one second
sleep(1);
}

// close file stream
fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
? >

PHP5 Tutorial - Defining Attributes of a PHP5 Class

In this tutorial you will learn about class attributes and how to declare & use them in PHP5 classes.

Definition of an class attribute
An attribute is also know as data members and is used to hold data of a class. The data that it holds are specific to the nature of the class in which it has been defined. For example, a Customer class would hold data related to a customer, an Order class would hold data related a an order.


Defining an attribute is as easy as declaring a variable within the body of the class. At the time of declaring a data member/variable within the body of the class, it is also possible to assign a default value to the attribute or data member.

Attributes can either be public, private or protected - the default being public. These are called Access Specifiers. You will learn more about access specifiers in PHP5 OOPS tutorials ahead.

Example Code:

class Customer {
private $first_name, $last_name;
private $outstanding_amount = 0; //example of default value

public function getData($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}

public function printData() {
echo $this->first_name . " : " . $this->last_name;
}
}

$c1 = new Customer();

In the above example $first_name, $last_name and $outstanding_amount are data members or attributes of the class Customer. Of these attributes, $outstanding_amount has a default value of zero assigned to it. This means that when an object of the Customer class has been created, $first_name and $last_name will be blank whereas; $outstanding_amount will be initialized with default value of zero.

Let’s take a problem statement and define a PHP5 Class for it.

Problem Statement:
Define a class to encapsulate Student. The data required to be captured is Student’s first name, last name, date of birth, address and telephone number.

Solution:

class Student {
public $first_name;
public $last_name;
public $date_of_birth;
public $address;
public $telephone;
}

In the above example $first_name, $last_name, $date_of_birth, $address and $telephone are all data members of class Student. These data members define the data that one Student Object will store. Therefore, you can create two objects of a Student class having different data as follows:

//creating an object $s1 of Student class and assigning data
$s1 = new Student();
$s1->first_name = "Sunil";
$s1->last_name = "Bhatia";
$s1->date_of_birth = "01/01/1900";
$s1->address = "India";
$s1->telephone = "9999999999";

//creating an object $s2 of Student class and assigning data
$s2 = new Student();
$s2->first_name = "Vishal";
$s2->last_name = "Thakur";
$s2->date_of_birth = "01/01/1910";
$s2->address = "USA";
$s2->telephone = "8888888888";

In the above example, we create two objects of the Student class $s1 and $s2. Both these objects have their own memory space within which they store their respective data. Because each data member is ‘public‘ you can directly access the data members of the Student Class using the arrow operator (reference operator) i.e. $s1->first_name = “Sunil”; You will learn more about public, private and protected in the PHP5 tutorials ahead on Access Specifiers.

The diagram below explains the above example:


PHP5 Tutorial - Learn to Create a PHP5 Class Object


In the earlier PHP5 OOPS tutorial you learnt how to create a class in PHP5. In this tutorial you will learn how to create an object of a PHP5 class. But before we begin, lets understand what is an object.

Definition of an Object
An object is a living instance of a class. This means that an object is created from the definition of the class and is loaded in memory. A good analogy to understand this is to compare objects with humans - and understand that all of us (you and I) are objects. If God wants to send a human to earth, what is easy for Him to do? Create and define properties and attributes of each human separately or create a one time template and generate objects out if it. Therefore, this onetime template is a Class and you, I & everyone in this world is an object - that is a living instance of class Human.


Creating Objects in PHP5 Class

To create an object of a PHP5 class we use the keyword new. Below is the syntax style of how to create objects in PHP5:

        $obj_name = new ClassName();

In the above syntax style, $obj_name is a variable in PHP. ‘new’ is the keyword which is responsible for creating a new instance of ClassName.

Look at the example below:

class Customer {
private $first_name, $last_name;

public function getData($first_name, $last_name) {
$this->first_name = $first_name;
$this->last_name = $last_name;
}

public function printData() {
echo $this->first_name . " : " . $this->last_name;
}
}

$c1 = new Customer();
$c2 = new Customer();

In the above example $c1 and $c2 are two objects of the Customer Class. Both these objects are allocated different blocks in the memory. Look at the diagram below:

Therefore, an object is a living instance of a class. Each object / living instance has its own memory space that can hold independent data values.

How to solve mysql ERROR 1118 (42000) Row size too large

  I had this issue with MYSQL 5.7 . The following worked althoug...