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

Friday, 29 December 2017

Today coding play ground is focused on string, number Concatenation using PHP, this is an interesting part

on our last topic we disccued on php Regular Expressions how we can validate emails, find words in string an all of that...

Today is also good, joining two or more words together is interesting and fun
for example

$full_name = $first_name . " " . $last_name; // results in "Marshall Unduemi"
Today we are going to work with php regluar expressions the good part of php, we use this expression pettern to gain text in a string, sometime we use this to validate email if it contain @ symbol or not.

The way you implement regular expression in other programming language does look alike to other languages.
For a complete reference,
see http://us2.php.net/manual/en/ref.pcre.php.

If are going to use Perl regular expressions on this excecis that will include forward slash '/' the the pattern.

For you find a matching pattern somewhere in a string
take a look at the example

if (preg_match("/ard/", "Harding"))
{
echo "Matches";
}
else
{
echo "No match";
}

Special symbols

\d any digit (0-9)
\s any white space (space, tab, EOL)
\w any word char (a-z, A-Z, 0-9, _)
. any character except EOL
[abc] a, b, or c
[^a-z] not any char between a and z


{3} match only three of these
? match zero or one character
* match zero or many characters
+ match one or many characters
^abc match at the very beginning of the string
abc$ match at the very end of the string


Email address pattern example

$email = 'first_name.last_name@domain.Com';
$regexp = "/^[\w.]+@[\w.]+[a-z]{2,4}$/i"; // i switch for case-insensitive match

if (preg_match($regexp, $email))
{
echo "Match email";
}

Remembering matched patterns


if (preg_match('/(\d\d):(\d\d) (am|pm)/', '03:15 pm', $matches)) {
echo "Hour: $matches[1]\n"; // 03
echo "Min: $matches[2]\n"; // 15
echo "Ending: $matches[3]\n"; // pm
}

Match all occurrences

preg_match_all('/.ar/', 'the car was far from the bar', $matches);
print_r($matches[0]); // Prints car, far, bar
Hay people, here is another tutorial for beginners, if you have started writing some php web pages, like function, and modules, sure is better to seperate codes to make it neat.

Today we  are going to talk about how you can add another web page to another one using PHP the universal Server language, by this means you can import Scripts and HTML Files.

A web page containing .php extension can or .html can can be imported using PHP build in function call 'require' with this you can import any web page to your current file.
 for example
 require 'filename.extension';

date.php
<h3>Today Date: <?php $today = date("D M d Y"); echo $today; ?></h3>

to import and display the script above is just to use the require function like this, this will produce an error if the date.php is not found.

<?php
        require 'date.php'; // print the current date
?>

The include statement does thesame thing, just that the difference is that if the date.php is not found it will not produce an error message to the user.

Now, the require_once and include_once accomplish thesame result, just that it will not reload a file which is already been reqyured or include.
Working with html form, these form inputs can be access using php superglobal arrays $_GET and $_POST.

$_GET: This get the value from the web url after the '?' sign for example www.site.com?id=1
 And if there is any space the system convert it to + sign for example www.site.com?name=Marshall+Unduemi&age=25;

$name = $_GET["name"]; // $name is "Marshall Unduemi"
$age = $_GET["age"]; // $age is 25

$_POST: for geting user data from html form using standard input this time the values passed along are un escaped.

Example www.site.com?name=Marshall+Unduemi&age=25;

$name = $_GET["name"]; // $name is "Marshall Unduemi"
$age = $_GET["age"]; // $age is 25

Is a best practice to avoid php warning and errors, the idea of using isset to check if the value passed is in $_POST OR $_GET method.

if (!isset($_POST["name"]) || trim($_POST["name"]) == "")
{
echo "The User's name was left blank.";
}

A shorcut to access variables in $_GET and $_POST: all you need to do is to extract $_POST OR $_GET all key/value pairs in identically
named variables for example

extract($_POST);
if (isset($name))
echo "Hello, $name!";

Hope this help you a little

Thursday, 28 December 2017

Array can have any size and contain any sort of value. No threat of going past exhibit limits. PHP arrays are associative arrays which permit component to be put away in connection to a key value instead of a strict straight record arrange.

$capitals["name"] = "John";
$capitals["AR"] = "Little Rock";

Initialize an array:
$colors = array("red", "green", "blue");

print("The 2nd color is $colors[1]."); // prints green

$capitals = array("name" => "John", "AR" => "Little Rock");

print("$capitals[name]"); // prints John, no quotes around key inside ""

E. Print contents of an array for debugging:
print_r($colors);

Produces:
Array
(
[0] => red
[1] => green
[2] => blue
)

print_r($capitals);
produces:
Array
(
[name] => John
[AR] => Little Rock
)

Pull values out of an array:
 
$colors = array("red", "green", "blue");
list($c1, $c2) = $colors;

print("$c1 and $c2"); // prints "red and green"

 Delete from an array:

unset($colors[1]); // $colors now contains red and blue at indexes 0 and 2.


Summary of all array functions in the PHP core: http://www.php.net/manual/en/ref.array.php