PHP Basics Part -2

PropellerAds

PHP And HTML Form
we consider you are having the basic knowledge of html and html form ,so here i am for quick revision giving the syntax of form with method post

<form action="data.php" method="post">//form budy </form>

 

Now lets see how to get value of form variable in php data.php file

<?php

$data=$_POST['varaiblename'];

?>

 

Syntax of form with method get

<form action="data.php" method="get">//form budy </form>

 

Now lets see how to get value of form variable in php data.php file

<?php

$data=$_GET['varaiblename'];

?>

 

Now lets See Some basics things we need to remember

How to Debug php code?

1.you can set error_reporting() so you can see the error message instead of a white page.You can use error_reporting() at the top of your code.

error_reporting(E_ALL);

2.PHP produces an error_log file in its directory whenever a problem occurs, you can find debug information there.

3.In most cases, if you set you error_report to -1 you’ll be able to see all notices, warning and errors in your browser

4., try using var_dump($someVarible). This will give you useful information about the current state of a variable – often better than echo

 

comments in php
To do a single line comment type “//” or “#” and all text to the right will be ignored by PHP interpreter.
The multiple line PHP comment begins with ” /* ” and ends with ” */ “.

Some Important PHP Functions to remember
PHP Date() Function
The PHP date() function convert a timestamp to a more readable date and time

date(format, timestamp)

 

PHP time() Function
The time() function is used to get the current time as a Unix timestamp


PHP mktime() Function
The mktime() function is used to create the timestamp based on a specific date and time. If no date and time is provided, the timestamp for the current date and time is returned
mktime(hour, minute, second, month, day, year)

Including a PHP File into Another PHP File
The include() and require() statement allow you to include the code contained in a PHP file within another PHP file.
require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue

 

Example for include
city.php

state.php

require_once statement
The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again.

The include_once statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE. As the name suggests, the file will be included just once.

Array Functions most commonly used

sizeof($array)
// Use this function to find out how many elements an array contains
array_values($array)
//Use this function to retrieve all the values from an associative array.
array_keys($array)
// Use this function to retrieve all the keys from an associative array.
array_pop($array)
//Use this function to remove an element from the end of an array.
array_push($array, $value)
// This function adds an element to the end of an array.
array_shift($array)
//This function removes an element from the beginning of an array.
array_unshift($array, $value)
// This function adds an element to the beginning of an array.
array_map($callback, $array1)
//This function returns an array containing all the elements of array1 after applying the callback function to each one. You can pass more than one array. The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()
sort($array)
// This function sorts the elements of an array in ascending order. String values will be arranged in ascending alphabetical order. Other sorting functions include asort(), arsort(), ksort(), krsort() and rsort().
array_reverse($array)
// This function reverses the order of elements in an array.
array_merge($array)
//Use this function when you need to combine data from two or more arrays into a single structure
array_rand($array)
// This function selects one or more random elements from an array.
array_search($search, $array)
//This function searches the values in an array for a match to the search term, and returns the corresponding key if found. If more than one match exists, the key of the first matching value is returned.
array_slice($array, $offset, $length)
//Use this function to break a larger array into smaller ones.
array_unique($array)
// Use this function when you need to remove non-unique elements from an array
extract($array)
//Import variables into the current symbol table from an array. For Better understanding read the doc about this function. It is very nice. Its opposite is compact().
array_key_exists($key, $array)
// This functions returns TRUE if the given key is set in the array.
in_array($searched_value, $array)
// This functions returns TRUE if the given value exists in the array

 

most commonly used String functions

substr()
//This function returns the part of the string as an output.
strlen()
//This function returns the length of the string
trim()
//This function removes the white-spaces from both start and the end of the string.
ltrim()
//This function removes the white-spaces from the left part of the string.
rtrim()
//This function removes the white-spaces from the right part of the string.
strtolower()
//This function converts the string to lower case
strtoupper()
//This function converts the string to upper case
str_replace()
//The str_replace() function replaces some characters with some other characters in a string.
explode()
// This function breaks the string into array on the basis of delimiter passed.
implode()
//This function join array elements with a string on the basis of delimiter passed.

 

File System Functions

basename() Returns the filename component of a path
chgrp() Changes the file group
chmod() Changes the file mode
chown() Changes the file owner
clearstatcache() Clears the file status cache
copy() Copies a file
delete() See unlink() or unset()
dirname() Returns the path of the parent directory
disk_free_space() Returns available space on filesystem or disk partition
disk_total_space() Returns the total size of a filesystem or disk partition
diskfreespace() Returns available space on filesystem or disk partition. Alias of disk_free_space()
fclose() Closes an open file pointer
feof() Tests for end-of-file on a file pointer
fflush() Flushes the buffered output to a file
fgetc() Returns a character from file pointer
fgetcsv() Gets line from file pointer and parse for CSV fields
fgets() Read a specific number of bytes from a file
fgetss() Reads a specific number of bytes from a file and strip HTML tags and PHP code
file() Reads entire file into an array
file_exists() Checks whether a file or directory exists
file_get_contents() Reads entire file into a string
file_put_contents() Write a string to a file
fileatime() Returns the last access time of a file
filectime() Returns the last change time of a file
filegroup() Returns the group ID of a file
fileinode() Returns the inode number of the file
filemtime() Returns the last modification time of a file
fileowner() Returns the user ID of the owner of the file
fileperms() Returns permissions for the file
filesize() Returns the file size
filetype() Returns the file type
flock() Locks or releases a file
fnmatch() Matches a filename or string against a specified pattern
fopen() Opens a file or URL
fpassthru() Output all remaining data on a file pointer
fputcsv() Format line as CSV and write to file pointer
fputs() Alias of fwrite()
fread() Reads a specific number of bytes from a file
fscanf() Parses input from a file according to a specified format
fseek() Seeks on a file pointer
fstat() Returns information about a file using an open file pointer
ftell() Returns the current position of the file read/write pointer
ftruncate() Truncates a file to a given length
fwrite() Writes the contents of string to the file pointer
glob() Returns an array of filenames/directories matching a specified pattern
is_dir() Checks whether the file is a directory
is_executable() Checks whether the file is executable
is_file() Checks whether the file is a regular file
is_link() Checks whether the filename is a symbolic link
is_readable() Checks whether a file exists and is readable
is_uploaded_file() Checks whether the file was uploaded via HTTP POST
is_writable() Checks whether the filename is writable
is_writeable() Alias of is_writable()
lchgrp() Changes group ownership of symlink
lchown() Changes user ownership of symlink
link() Create a hard link
linkinfo() Returns information about a link
lstat() Returns information about a file or symbolic link
mkdir() Creates a directory
move_uploaded_file() Moves an uploaded file to a new location
parse_ini_file() Parse a configuration file
parse_ini_string() Parse a configuration string
pathinfo() Returns information about a file path
pclose() Closes process file pointer
popen() Opens process file pointer
readfile() Reads a file and writes it to the output buffer
readlink() Returns the target of a symbolic link
realpath() Returns canonicalized absolute pathname
realpath_cache_get() Returns realpath cache entries
realpath_cache_size() Returns realpath cache size
rename() Renames a file or directory
rewind() Rewind the position of a file pointer
rmdir() Removes an empty directory
set_file_buffer() Sets the buffer size of a file
stat() Returns information about a file
symlink() Creates a symbolic link
tempnam() Create temporary file with unique file name
tmpfile() Creates a unique temporary file
touch() Sets access and modification time of file
umask() Changes the current umask
unlink() Deletes a file

 

PHP File upload function
move_uploaded_file — Moves an uploaded file to a new location
Syntax
bool move_uploaded_file ( string $filename , string $destination )

PHP Cookies functions
The setcookie() function is used to set a cookie in PHP. Make sure you call the setcookie() function before any output generated by your script otherwise cookie will not set.
setcookie(name, value, expire, path, domain, secure);

getting Cookies Values

Removing Cookies

PHP Session
A session is a way to store information (in variables) to be used across multiple pages.
A session is started with the session_start() function.
all session variable values are stored in the global $_SESSION variable
Set session variables $_SESSION[“myname”] = “Joen”;
Echo session variables that were set on previous page

// Echo session variables that were set on previous page
echo "my name is " . $_SESSION["myname"] . ".
";