Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Simplified PHP
Simplified PHP
Simplified PHP
Ebook252 pages1 hour

Simplified PHP

Rating: 0 out of 5 stars

()

Read preview

About this ebook

What this book is about, is not to teach you how to program in PHP but to show you more structured way to improve what you're already doing by reusing code and building better functions that will be useful in the long term. Beyond that, working with code that is easily readable and understandable by just applying a few programming practices. This doesn't produce the most efficient code but it does allow you to completely understand what you've written before, to easily make changes at any time.
Probably most importantly providing a different aspect to how you program.
It's time to stop reinventing the wheel every time you have to build a new website. As well to provided list of commands that you are likely to use every day including how to use them and what the parameters mean.

LanguageEnglish
Release dateJan 6, 2019
ISBN9781370654956
Simplified PHP
Author

James Blanchette

He has spent more than 35 years in the computer industry, developing and programming in multiple programming languages. Currently he is working mostly in Website and Application development for Android systems. Website design with full PHP backend for dynamic websites.

Read more from James Blanchette

Related to Simplified PHP

Related ebooks

Programming For You

View More

Related articles

Reviews for Simplified PHP

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Simplified PHP - James Blanchette

    Simplified PHP

    James Blanchette PhD

    copyright James Blanchette 2018

    Published by Spangaloo Publishing

    Spangaloo Edition

    Standard Copyright eBooks are strictly protected works. You must not perform any actions, including copying, printing and distribution without the author’s written or printed consent (the author may have already granted certain terms in a statement within this book.) Some of our eBooks are cleared for personal printing if this option has been enabled, The unauthorized sale of Copyright works in any form is illegal.

    Names and companies are used for reference purposes only and are not intended to be an endorsement

    Cover Design: James Bryron Love

    Ebook Formatting : Alan Thriete

    http://spangaloo.com

    Contents

    Preface

    What is PHP

    PHP Basics

    IF ELSE ELSEIF

    WHILE

    DO WHILE

    FOR NEXT

    FOREACH

    SWITCH

    TRIM

    STRINGS

    str_replace

    explode

    String Change case strtolower() strtoupper() ucfirst() ucwords()

    substr

    strpos

    strlen

    strstr

    ARRAYS

    ISSET

    COUNT

    TIME

    SESSIONS

    MYSqli

    MySqli Connect

    Mysqli Query

    Classes

    Some simple rules

    Reusable pages

    Reusable code

    Generic functions

    HTML with PHP

    CSS and PHP

    Controlling User Input

    Security

    Harden Your PHP

    Sessions

    Website

    Database

    Building Websites

    AJAX

    Error Control

    Appendix List

    Mysql Data Types

    MYSQL Keywords and Reserved Words

    List of Keywords PHP

    String Functions List

    Time Zone List

    Date Format Characters List

    Other Books

    Preface

    What this book is about, is not to teach you how to program in PHP but to show you more structured way to improve what you’re already doing by reusing code and building better functions that will be useful in the long term. Beyond that, working with code that is easily readable and understandable by just applying a few programming practices. This doesn’t produce the most efficient code but it does allow you to completely understand what you’ve written before, to easily make changes at any time.

    Probably most importantly providing a different aspect to how you program.

    It’s time to stop reinventing the wheel every time you have to build a new website. As well to provided list of commands that you are likely to use every day including how to use them and what the parameters mean.

    What is PHP?

    From the preface of the manual:

    PHP is an HTML-embedded scripting language. Much of its syntax is borrowed from C, Java and Perl with a couple of unique PHP-specific features thrown in. The goal of the language is to allow web developers to write dynamically generated pages quickly.

    What does PHP stand for?

    PHP stands for PHP: Hypertext Preprocessor. This confuses many people because the first word of the acronym is the acronym. This type of acronym is called a recursive acronym.

    I’d like to start by explaining that PHP is an incredibly complex language, capable of most if not all the things that you would like it to do.

    Everything from robust classes to give you object orientated programming, to simply displaying the date on a website. PHP can be utilized using Ajax for dynamic web development that encompasses both server-side and client-side.

    Most problems stem from trying to fix code that you wrote six months ago. As you look at it is more difficult to glean any kind of reference to what you are thinking about at the time.

    Embedding comments in your files is something that is not only helpful, it is absolutely necessary especially if you need to do some rewrites.

    PHP is constantly evolving and as such various commands are either improved and changed or disregarded completely and no longer available.

    Simplifying the writing of PHP, to make it easier to understand in the future is what we are going to be talking about. Simplifying the PHP does not make it as efficient as some more complicated versions of the code can be, however readability is what we’re after.

    Bear in mind that simplifying PHP is not just about writing readable code, it’s about being able to reuse the code over and over again thus saving you time, energy and frustration.

    PHP Basics

    This chapter will outline some of the basics of PHP including some of the necessary commands and keywords that you will use it continuously throughout your PHP programming. You can skip this particular chapter if your are you familiar with most of the PHP concepts.

    IF ELSE ELSEIF

    The if construct or command is available in almost all programming languages. It allows for conditional execution of code. The IF structure itself is similar to that of C programming language.

    The expression is evaluated to its boolean value and if the expression is true then the rest of the statement is executed, if it is false that everything gets ignored.

    if (expr)

    statement

    if (condition) {

    code to be executed if condition is true;

    }

    An example.

    $t = date(H);

    if ($t <= 20) {

    echo Have a good day!;

    }else{

    echo Have a good night!;

    }

    By using the else command when the expression is false, we can still do something. It could be to generate an error or output something completely different.

    ELSEIF

    if (condition) {

    code to be executed if this condition is true

    } elseif (condition) {

    code to be executed if this condition is true;

    } else {

    code to be executed if all conditions are false;

    }

    if ( isset ($_SERVER[HTTP_X_FORWARDED])){

    $testip = $_SERVER[HTTP_X_FORWARDED];

    }elseif ( isset ($_SERVER[HTTP_FORWARDED_FOR])){

    $testip = $_SERVER[HTTP_FORWARDED_FOR];

    }elseif ( isset ($_SERVER[HTTP_FORWARDED])){

    $testip = $_SERVER[HTTP_FORWARDED];

    }elseif ( isset ($_SERVER[HTTP_X_FORWARDED])){

    $testip = $_SERVER[HTTP_X_FORWARDED];

    }else{

    if ( isset ($_SERVER[REMOTE_ADDR])){

    $testip = $_SERVER[REMOTE_ADDR];}

    }

    The above code steps through a list of possibilities to grab the user’s IP address.

    $t = date(H);

    if ($t <= 10) {

    echo Have a good morning!;

    } elseif ($t <= 20) {

    echo Have a good day!;

    } else {

    echo Have a good night!;

    }

    Special characters.

    The IF statement generally uses two characters in determining whether a statement is true or not. If it is going to equal something then best practice suggest you use a double Equal sign ==. There are other characters involved and well as can be seen in the above code using the less than <= but also available is the greater than >=. Others are using both greater than and less than two equate to not equal <> as well this != also means not equal to.

    There are other ways to express the same thing in a shorter version. Consider this.

    if ($condition) {

    $result = ‘foo’

    } else {

    $result = ‘bar’

    }

    can also be expressed this way.

    $result = $condition ? ‘foo’ : ‘bar’;

    If this $condition evaluates to true, the lefthand operand will be assigned to $result. If the condition evaluates to false, the righthand will be used. As well you can use additional equations as part of a single if statement by using logical operators. You have and, or, XOR,&& || and ! Each performing its own way.

    if (x==1 and y=2){

    }

    if (x==1 && y=2){

    }

    if (x==1 or y=2){

    }

    Special conditions also exist , using the triple equal is not the same as using a double equal

    if($a == $b){

    // do something

    }

    Equal true: if $a is equal to $b, after type juggling.

    if($a === $b){

    // do something

    }

    Identical true: if $a is equal to $b, and they are of the same type. So what that means is that if both are integers or perhaps floating-point as well as a string type they can be compared by their type to evaluate whether they’re true or not. If you mix an imager with a string and use the triple equal it will always be false.

    WHILE

    The while command is used in loops and will continue to loop as long as a given condition is true. It also follows a similar structure to the IF command.

    while (expr)

    statement

    The meaning of the while command is simple PHP will execute the necessary men’s repeatedly as long as the while at expression evaluates to true. The value is checked at the beginning of the loop so even if the value changes during execution execution will not stop until the end of the iteration.

    If the expression evaluates to false then the while command will not execute any code.

    $x = 1;

    while ($x <= 10) {

    echo $x++;

    /* the printed value would be $x before the increment (post-increment) */

    }

    As well without curly braces.

    $x = 1;

    while ($x <= 10):

    echo $x;

    $x++;

    endwhile;

    Database access heavily utilizes the while command as seen below.

    $querysub = select * from sometable order by rand() limit 5;

    $resultSub = mysqli_query($con,$querysub) or die(mysqli_error($con)) ;

    if(mysqli_num_rows($resultSub)>=1)

    {

    //$output[];

    while ($getvalue = mysqli_fetch_array($resultSub) )

    {

    $id=$getvalue[id];

    $value=trim($getvalue[word]);

    // do something with the data

    }

    }

    This particular query will loop five times because we’ve set the limit in

    Enjoying the preview?
    Page 1 of 1