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.
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
Plato Raw Rating: 0 out of 5 stars0 ratingsTheO 2 Rating: 0 out of 5 stars0 ratingsSimplified PHP Rating: 0 out of 5 stars0 ratingsTheO Rating: 0 out of 5 stars0 ratingsSet Up your own Twitter APP Rating: 0 out of 5 stars0 ratings
Related to Simplified PHP
Related ebooks
PHP Mastery: Crafting Dynamic Web Solutions Rating: 0 out of 5 stars0 ratingsPHP & MYSQL: 200 Solved Exercises and Projects for Practice Rating: 0 out of 5 stars0 ratingsPHP 8: The Modern Web Developer's Guide Rating: 0 out of 5 stars0 ratingsReference to PHP, Second Edition Rating: 0 out of 5 stars0 ratingsBeginning JavaScript Rating: 0 out of 5 stars0 ratingsPhpStorm Cookbook Rating: 0 out of 5 stars0 ratingsThe PHP Workshop: Learn to build interactive applications and kickstart your career as a web developer Rating: 0 out of 5 stars0 ratingsIntroduction to PHP Web Services: PHP, JavaScript, MySQL, SOAP, RESTful, JSON, XML, WSDL Rating: 0 out of 5 stars0 ratingsLearn PHP: Learn PHP Programming in 4 hours! PHP for Beginners - Smart and Easy Ways to learn PHP & MySQL Rating: 4 out of 5 stars4/5Learning PHP 7 High Performance Rating: 0 out of 5 stars0 ratingsPHP Programming For Beginners: The Simple Guide to Learning PHP Fast! Rating: 0 out of 5 stars0 ratingsPHP BLUEPRINT: An Essential Beginners Guide to Learn the Realms of PHP From A-Z Rating: 0 out of 5 stars0 ratingsPHP Error Reporting: How To Do It Right Rating: 0 out of 5 stars0 ratingsLearn PHP Programming in 7Days: Ultimate PHP Crash Course For Beginners Rating: 3 out of 5 stars3/5PHP for Beginners Rating: 0 out of 5 stars0 ratingsLearning Magento 2 Administration Rating: 0 out of 5 stars0 ratingsUltimate Tailwind CSS Handbook: Build sleek and modern websites with immersive UIs using Tailwind CSS Rating: 0 out of 5 stars0 ratingsWeb Development Essentials: A Beginner's Guide to HTML, CSS, JavaScript, and SEO: HTML 101 : Beginner to pro Rating: 0 out of 5 stars0 ratingsPHP 5 CMS Framework Development - 2nd Edition Rating: 0 out of 5 stars0 ratingsJump Start Web Performance Rating: 0 out of 5 stars0 ratingsHTML5 Games: Creating Fun with HTML5, CSS3 and WebGL Rating: 0 out of 5 stars0 ratingsIan Talks CSS A-Z: WebDevAtoZ, #3 Rating: 0 out of 5 stars0 ratingsResponsive Design High Performance Rating: 0 out of 5 stars0 ratingsMySQL 8 Cookbook: Ready solutions to achieve highest levels of enterprise database scalability, security, reliability, and uptime Rating: 0 out of 5 stars0 ratingsModernizing Legacy Applications in PHP Rating: 0 out of 5 stars0 ratingsMastering Sass Rating: 0 out of 5 stars0 ratingsPHP Ajax Cookbook Rating: 2 out of 5 stars2/5Learning PHP Data Objects Rating: 5 out of 5 stars5/5
Programming For You
Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python Data Structures and Algorithms Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsSQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5How To Become A Data Scientist With ChatGPT: A Beginner's Guide to ChatGPT-Assisted Programming Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Beginning Programming with C++ For Dummies Rating: 4 out of 5 stars4/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5PYTHON PROGRAMMING Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Godot from Zero to Proficiency (Foundations): Godot from Zero to Proficiency, #1 Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python 3 Object Oriented Programming Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Microsoft Azure For Dummies Rating: 0 out of 5 stars0 ratingsPython for Data Science For Dummies Rating: 0 out of 5 stars0 ratings
Reviews for Simplified PHP
0 ratings0 reviews
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
––––––––
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[];