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

PHP for circles execute a piece of code a predefined number of times. The PHP for Loop. The for circle is utilized when you know ahead of time how frequently the content should run.

Looping structures

1. while ($n < 10) {
print("$n ");
$n++;
}

2. do {
print("$n ");
$n++;
}
while ($n < 10);

3. for ($n = 1; $n < 10; $n++)
print("$n ");

4. foreach ($myarray as $item)
print("$item ");
Howdy my kin, today subject is interesting like no other php put forth basic with if expression, without much talk,  if statement are Control Structures in PHP usein making decisions.

if statement control block

if(condition)
{
statement
}

for example

1. if ($x > 0)
{

$y = 5;

}

Code explained: if $x as a value is greater than 0 as a value then assign 5 to y variable

Now, let take a look at another sample code to broaden our understanding more on this context.

2. if($username==$logusername)
{
    echo "Username Correct!";
}
or
if($access==0)
{
    echo "Access denied!";
}

    This time we will be dealing with if else statement

3. if ($a)
{
// print if $a is true or not
print($b);
$b++;
}
else
print($c);


4. if ($a > $b)
print "a is greater than b";
elseif ($a == $b) // use "elseif" or "else if"
print "a is equal to b";
else
print "a is less than b";

5. switch ($vehicle_type) { // works for integers, floats, or strings
case "car":
$car++;
break;

case "truck":
$truck++;
 break;

case "suv":
$suv++;
break;

default:
$other++;
}

We are going to talk more on switch later on...

On this instructional exercises we will find out what are Operators is all about, we use operators for assignment, Arithmetic, Comparison, Logical statement.

Assignment Operators Includes
1. = += -= /= *= %= ++ -- : unlike other programming languages.
2. .= - joining words operator.

for exmple

$text="This is a text";
$increment+=2;// increment by 2;
$decre-=1; decreament by 1;
$num1/=5; // the value of num1 divide by 5
$num2*=10; //the value of num2 multiplyby 10
$num3++; // auto increment by 1 at every run
$num3--; // auto decrement by 1 at every run

2. .= : joining words operator.

$string1.=' php world!'; // the space there matters a lot


Arithmetic Operators Includes
1. + - * / % - unlike other programming languages.
simply use for for calculation purpose

$result=$val1 + $val2; // add the value of 1 and 2
$result=$val1 - $val2; // subtract the value of 1 from 2
$result=$val1 * $val2; // add the value of 1 and 2
$result=$val1 / $val2; // divide the value of 1 from 2
$result=$val1%; // get the percentage the value of 1

Comparison Operators Includes
Is use to compare and contrast between two values, also can be use for decission making process, like if, else if, switch etc
1. == != < > <= >= : Unlike other programming languages. Even less than < and greater than > is thesame as !=.

for exmaple
1. if($val==$val2)
{
    print 'result';
}

2.  if($val!=$val2)
{
    print 'result';
}
Note: the exclamation sign ! means is not equal to.

3.  if($val < $val2) // less than
{
    print 'result';
}

4.  if($val > $val2) // greater than
{
    print 'result';
}

5.  if($val <= $val2) // less than or equal to, two condition are pased here.
{
    print 'result';
}

6.  if($val >= $val2) // greater than or equal to, two condition are also pased here.
{
    print 'answer';
}



2. === : this type is used to valid datatype and value if datatypes are thesame.
for example
$val=10;
$val2=9;
if($val===$val2)
{
    print 'Condition Passed';
}
else
{
    print 'The value '.$val.' is not equal to '.$val2;
}

3. !== - this other is the case, is also use thesame way but this time is not thesame datatype because of ! sign.

if($val!==$val2) // like the above example using ! sign you know what that means, is not equal
{
        print 'The value '.$val.' is not equal to '.$val2;
}
else
{
        print 'Condition Passed';
}

for exmaple
if($val==$val2)

Logical Operators Includes
As the name says logic

1. && || ! : Unlike other programming languages (&& and || short-form)
2. and or : like && and || but this || have lower precedence than &&

For Example
1. if($a==3 && $b=6)
{
    print 'All the condition are passed!';
}
or
if($a==3 AND $b=6)
{
    print 'All the condition are passed!';
}

OR STATEMENT
if($a==3 || $b=6)
{
    print 'One of the condition is passed!';
}

if($a==3 OR $b=6)
{
    print 'One of the condition is passed!';
}
Hello! friends today exercise will be interesting as we are going to work on the fun part of php function, this will let us to declare and use function.

how to create your own php function, see code

function functionName(parameters)
{
    statement;
}

simple right? sure

Now let create one and test it, open your code editor and copy the below code to your editor, save as function.php and run your localhost see result

function helloWorld()
{
    $text="Hi! Hello World!";

    return $text;
}

we have just created our php helloWorld function, this will not output anything untill we call that function, here is how

echo helloWorld();

This will now output the hello World message to the user
Other examples

function Add($num1,$num2)
{
    $result=$num1+$num2;

    return $result;
}

echo Add(10,23);

or

$num1=10;
$num2=23;
echo Add($num1,$num2);

if you don't understand about variable and datatype click here
Hello guys today we are going to work with php variable and data types the fun aspect of PHP, what you need is and editor and php engine(Apache Server) to power our script to run.

To declare a variable in php is really simple, use the dollar sign($) followed by a name of your choice that relate to your context, for example

$storeName="Your name";

Because i want t store string/letter, make sure you use double quote when you are dealing with string or letter.

Always follow variable naming principle, use underscore, don't start with number, or hyphen or any special character such as this @#$%^&*()-=+

Example Two:

To store integer or float number see sample code
$MyNumber=23;
$myFloat=299.00;

We are going to deal with function later on, that will bring us to - local scope

If you don't want your code to get error simply use semi-colon to terminate after everything, this tell the system that this line of code has done it job.