Simplified PHP
()
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
TheO Rating: 0 out of 5 stars0 ratingsPlato Raw Rating: 0 out of 5 stars0 ratingsSimplified PHP 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 Programming For Beginners: The Simple Guide to Learning PHP Fast! Rating: 0 out of 5 stars0 ratingsPHP programming Rating: 0 out of 5 stars0 ratingsIntroduction to PHP, Part 5, Second Edition Rating: 0 out of 5 stars0 ratingsPHP Examples Part 3 Rating: 5 out of 5 stars5/5Introduction to PHP, Part 4, Second Edition Rating: 0 out of 5 stars0 ratingsJust the basics of JavaScript Rating: 0 out of 5 stars0 ratingsPHP for Beginners Rating: 0 out of 5 stars0 ratingsReference to PHP, Second Edition Rating: 0 out of 5 stars0 ratingsJavaScript Interview Questions You'll Most Likely Be Asked: Job Interview Questions Series Rating: 0 out of 5 stars0 ratingsJavaScript Patterns JumpStart Guide (Clean up your JavaScript Code) Rating: 4 out of 5 stars4/5PHP Examples, Part 2 Rating: 1 out of 5 stars1/5PHP Examples Part 4 Rating: 0 out of 5 stars0 ratingsIntroduction to PHP Rating: 3 out of 5 stars3/5Introduction to PHP, Part 2, Second Edition Rating: 0 out of 5 stars0 ratings50 Recipes for Programming Node.js Rating: 3 out of 5 stars3/5Rapid Application Development With CakePHP Rating: 0 out of 5 stars0 ratingsJava: Best Practices to Programming Code with Java Rating: 0 out of 5 stars0 ratingsJava: Best Practices to Programming Code with Java: Java Computer Programming, #3 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 ratingsPHP B L U E P R I N T: Advanced Guide to Learn the Realms of PHP Programming Rating: 0 out of 5 stars0 ratingsJavaScript: Advanced Guide to Programming Code with Javascript: JavaScript Computer Programming, #4 Rating: 0 out of 5 stars0 ratingsJavaScript: Advanced Guide to Programming Code with JavaScript Rating: 0 out of 5 stars0 ratingsLearn JavaScript in 24 Hours Rating: 3 out of 5 stars3/5Practical Web Development with Haskell: Master the Essential Skills to Build Fast and Scalable Web Applications Rating: 0 out of 5 stars0 ratingsLearn PHP in 24 Hours Rating: 0 out of 5 stars0 ratingsTeach Yourself Programming The Guide to Programming & Coding Like a Professional Rating: 0 out of 5 stars0 ratingsLearn Programming by Coding Like a Professional: Create Games, Apps, & Programs Rating: 0 out of 5 stars0 ratingsJavascript - 50 functions and tutorial Rating: 0 out of 5 stars0 ratingsBE EXPERT IN JAVA Part- 2: Learn Java programming and become expert Rating: 0 out of 5 stars0 ratingsLearn Programming Using C# Rating: 0 out of 5 stars0 ratings
Programming For You
Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days 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/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5SQL: For Beginners: Your Guide To Easily Learn SQL Programming in 7 Days Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Pokemon Go: Guide + 20 Tips and Tricks You Must Read Hints, Tricks, Tips, Secrets, Android, iOS Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards Rating: 0 out of 5 stars0 ratingsWeb Designer's Idea Book, Volume 4: Inspiration from the Best Web Design Trends, Themes and Styles Rating: 4 out of 5 stars4/5101 Amazing Nintendo NES Facts: Includes facts about the Famicom Rating: 4 out of 5 stars4/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Learn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS 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
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