SlideShare a Scribd company logo
1 of 8
Omar Bin Sulaiman
PHP Tips and Tricks by
Introduction
• In previous articles we've touched on some major sections such
as MySQLi, Classes and cURL. There are a lot of other tips and
tricks that you should look at, and a few of them are covered
below, a couple of which are transferrable between languages.
Ternary Operators
• A ternary operator is a different way to layout a simple if
statement. Let's take a look at one and then break it down. We'll
just use a simple check to see if a GET parameter is set and not
empty, if it is we will default to the word John, otherwise use the
value that was set as the
GET parameter.
• $name = (!empty($_GET['name'])? $_GET['name'] : 'John');
This might look complex to begin with but there are three
sections to a ternary operator:
The conditional !empty($_GET['name'])
The true result $_GET['name']
The false result 'John'
The ternary operator will return either the true or false
conditional, and therefore it will be assigned to $name. If we
write this as a normal if statement we'd have one of the
following:
if(!empty($_GET['name'])){ $name = $_GET['name']; }else{ $name
= 'John'; } // Or $name = 'John'; if(!empty($_GET['name']))
{ $name = $_GET['name']; }
The two above and the ternary operator will all end up with
$name having something assigned to it.
Nesting
• While not advisable, it is possible to nest ternary operators within
each other much like you would with standard if statements, or
to form a long string of if...else if...else if...else statements.
• Ternary operators can be very helpful and clean up code, but
don't over complicate them otherwise your code might become
inundated with large amounts of complex rediculousness that
will get you hunted down by a violent psychopath.
Class Autoloading
• To make use of a class file, you have to ensure that the class
you're using is available within the page that you're using it. In
most cases this will be as easy as you just including the PHP file
that contains the class.
• But what if we have a very large number of classes that we
could potentially use within one or many sections of our code?
Well, we could include all of them within a common header file,
or only include the ones that we know that we're going to use on
that page. However, we then need to remember to include new
classes whenever we want to use a different one etc.
• So instead of including classes that might not get used, or we
might remove later, we can employ class autoloading. When we
autoload classes we can write what will happen - this might be
to just throw an error to let us know that we've not included the
class file, or, as in most cases, we make the function include the
file that we need.
The Code
• Here we'll be using the PHP function spl_autoload_register(), we'll only
be interested in the first parameter, but there are two others that you
can define if you wanted the function to behave differently. I'll jump
straight into how to use it:
• function loadMyClass($class){ echo 'We are loading class: ' . $class;
include_once('classes/' . $class . '.inc.php'); echo 'Class loaded.'; }
spl_autoload_register('loadMyClass');
This will pass the name of the class that we're trying to load as the first
parameter of the function that we specified. As of PHP 5.3.0 we are
able to use anonymous functions, basically a function thas has no
name:
• spl_autoload_register(function ($class){ echo 'We are loading class: ' .
$class; include_once('classes/' . $class . '.inc.php'); echo 'Class
loaded.'; });
• Which will work exactly the same as the method of implementation
above. So now in future, we don't need to include the class files that
we'll be using as this function will run if the class is undefined, include
the class and then the instantiation will be called.
Dynamic Variables
• Sometimes we want to be able to dynmically define a variable, or refer
to a variable based on a user's input. Let's start with this code:
• $myname = 'Michael'; $yourname = 'Ben'; if($_GET['whatname'] ==
'your'){ echo $yourname; }else if($_GET['whatname'] == 'my'){ echo
$myname; }else{ echo 'No Name'; }
To use the value of a variable to then form another variable we use
double dollar signs $$ so for example we could do $who = 'myname';
then echo $$who. This will echo out the value of $myname.
• Break it down like this (exemplary code, not actual working code):
$who = 'myname'; $$who = $'myname'; $$who = $myname; echo $
$who; // would return 'Michael'
In reference to the code above we could look to use something along
the lines of the following for very dynamically defined variables:
• $who = $_GET['whatname'] . 'name'; if(isset($$who)){ echo $$who; }
else{ echo 'No Name'; }
As with ternary operators, you should use these loosely, as in most
situations there's not much need for them, but you will most probably
find some crazy-ninja somewhere who uses this notation.
Thank You
Omar Bin Sulaiman

More Related Content

What's hot

Php 101 by David Menendez
Php 101 by David MenendezPhp 101 by David Menendez
Php 101 by David Menendezdm09f
 
Learning puppet chapter 2
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2Vishal Biyani
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the BasicsMichael Koby
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04Spy Seat
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntaxDhani Ahmad
 
4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!TargetX
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)Dave Cross
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesNina Zakharenko
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST FrameworkLoad Impact
 

What's hot (18)

Php 101 by David Menendez
Php 101 by David MenendezPhp 101 by David Menendez
Php 101 by David Menendez
 
PHP-Part4
PHP-Part4PHP-Part4
PHP-Part4
 
Oops in php
Oops in phpOops in php
Oops in php
 
Learning puppet chapter 2
Learning puppet chapter 2Learning puppet chapter 2
Learning puppet chapter 2
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Ruby: Beyond the Basics
Ruby: Beyond the BasicsRuby: Beyond the Basics
Ruby: Beyond the Basics
 
Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
4.2 PHP Function
4.2 PHP Function4.2 PHP Function
4.2 PHP Function
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!4.7 Automate Loading Data with Little to No Duplicates!
4.7 Automate Loading Data with Little to No Duplicates!
 
Perl Teach-In (part 2)
Perl Teach-In (part 2)Perl Teach-In (part 2)
Perl Teach-In (part 2)
 
Intro to Perl
Intro to PerlIntro to Perl
Intro to Perl
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Php go vrooom!
Php go vrooom!Php go vrooom!
Php go vrooom!
 
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 MinutesDjangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
Djangocon 2014 - Django REST Framework - So Easy You Can Learn it in 25 Minutes
 
Django REST Framework
Django REST FrameworkDjango REST Framework
Django REST Framework
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 

Viewers also liked

Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)beaumartin1127
 
Keshu powerpoint presentation
Keshu powerpoint presentationKeshu powerpoint presentation
Keshu powerpoint presentationkesu1234
 
Chudidar stitching
Chudidar stitchingChudidar stitching
Chudidar stitchingLatha Samy
 
Redesigning female motorcycle wear
Redesigning female motorcycle wearRedesigning female motorcycle wear
Redesigning female motorcycle wearCarmen Schweizer
 
Evaluation question 4
Evaluation question 4Evaluation question 4
Evaluation question 4SeyiiO
 
Mod 4.3- Consolidating your learning
Mod 4.3- Consolidating your learningMod 4.3- Consolidating your learning
Mod 4.3- Consolidating your learningmelissaGH
 
Doc B Boeing Certs Coursework COMPLETE 26OCT2016
Doc B Boeing Certs Coursework COMPLETE 26OCT2016Doc B Boeing Certs Coursework COMPLETE 26OCT2016
Doc B Boeing Certs Coursework COMPLETE 26OCT2016Rod B
 
Media new
Media new Media new
Media new courtxo
 
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaa
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaaKMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaa
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaaA-lehdet Oy
 
Maneras de comunicar información
Maneras de comunicar informaciónManeras de comunicar información
Maneras de comunicar informaciónJOSUEELIAN
 
Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)beaumartin1127
 
nitalistari resume
nitalistari resumenitalistari resume
nitalistari resumenita listari
 

Viewers also liked (20)

Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)
 
Presentation ICCM2016
Presentation ICCM2016Presentation ICCM2016
Presentation ICCM2016
 
Keshu powerpoint presentation
Keshu powerpoint presentationKeshu powerpoint presentation
Keshu powerpoint presentation
 
Chudidar stitching
Chudidar stitchingChudidar stitching
Chudidar stitching
 
Redesigning female motorcycle wear
Redesigning female motorcycle wearRedesigning female motorcycle wear
Redesigning female motorcycle wear
 
Evaluation question 4
Evaluation question 4Evaluation question 4
Evaluation question 4
 
O.12
O.12O.12
O.12
 
Mod 4.3- Consolidating your learning
Mod 4.3- Consolidating your learningMod 4.3- Consolidating your learning
Mod 4.3- Consolidating your learning
 
Smog
SmogSmog
Smog
 
Daniel lozano estupiñan
Daniel lozano estupiñanDaniel lozano estupiñan
Daniel lozano estupiñan
 
Doc B Boeing Certs Coursework COMPLETE 26OCT2016
Doc B Boeing Certs Coursework COMPLETE 26OCT2016Doc B Boeing Certs Coursework COMPLETE 26OCT2016
Doc B Boeing Certs Coursework COMPLETE 26OCT2016
 
Media new
Media new Media new
Media new
 
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaa
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaaKMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaa
KMT-aamu 3.11.2015: Mitä aikakauslehtien uusi KMT parhaimmillaan tarjoaa
 
Resume2016
Resume2016Resume2016
Resume2016
 
Presentación1
Presentación1Presentación1
Presentación1
 
Maneras de comunicar información
Maneras de comunicar informaciónManeras de comunicar información
Maneras de comunicar información
 
Euroheat Brand guide
Euroheat Brand guideEuroheat Brand guide
Euroheat Brand guide
 
Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)Educational apps slideshow (sossaman, peery, martin)
Educational apps slideshow (sossaman, peery, martin)
 
Keshu
KeshuKeshu
Keshu
 
nitalistari resume
nitalistari resumenitalistari resume
nitalistari resume
 

Similar to Php tips and tricks by omar bin sulaiman

PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.pptJamers2
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comayandoesnotemail
 
powerpoint 2-7.pptx
powerpoint 2-7.pptxpowerpoint 2-7.pptx
powerpoint 2-7.pptxJuanPicasso7
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHPBUDNET
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_masterjeeva indra
 
Task 1
Task 1Task 1
Task 1EdiPHP
 
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02sos informatique
 
php fundamental
php fundamentalphp fundamental
php fundamentalzalatarunk
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of phppooja bhandari
 

Similar to Php tips and tricks by omar bin sulaiman (20)

PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
PHP-03-Functions.ppt
PHP-03-Functions.pptPHP-03-Functions.ppt
PHP-03-Functions.ppt
 
Oop in php_tutorial
Oop in php_tutorialOop in php_tutorial
Oop in php_tutorial
 
PHP - Introduction to PHP Functions
PHP -  Introduction to PHP FunctionsPHP -  Introduction to PHP Functions
PHP - Introduction to PHP Functions
 
Php, mysq lpart1
Php, mysq lpart1Php, mysq lpart1
Php, mysq lpart1
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
powerpoint 2-7.pptx
powerpoint 2-7.pptxpowerpoint 2-7.pptx
powerpoint 2-7.pptx
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Oop's in php
Oop's in php Oop's in php
Oop's in php
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
Task 1
Task 1Task 1
Task 1
 
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02
Howdoesgettemplatepartworksintwentytentheme 110123234953-phpapp02
 
05php
05php05php
05php
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 

Recently uploaded

Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Servicesexy call girls service in goa
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
Radiant Call girls in Dubai O56338O268 Dubai Call girls
Radiant Call girls in Dubai O56338O268 Dubai Call girlsRadiant Call girls in Dubai O56338O268 Dubai Call girls
Radiant Call girls in Dubai O56338O268 Dubai Call girlsstephieert
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Gram Darshan PPT cyber rural in villages of india
Gram Darshan PPT cyber rural  in villages of indiaGram Darshan PPT cyber rural  in villages of india
Gram Darshan PPT cyber rural in villages of indiaimessage0108
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Roomishabajaj13
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneRussian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneCall girls in Ahmedabad High profile
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607dollysharma2066
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girlsstephieert
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Roomgirls4nights
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Roomdivyansh0kumar0
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Roomdivyansh0kumar0
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Delhi Call girls
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirtrahman018755
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.soniya singh
 

Recently uploaded (20)

Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine ServiceHot Service (+9316020077 ) Goa  Call Girls Real Photos and Genuine Service
Hot Service (+9316020077 ) Goa Call Girls Real Photos and Genuine Service
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
Radiant Call girls in Dubai O56338O268 Dubai Call girls
Radiant Call girls in Dubai O56338O268 Dubai Call girlsRadiant Call girls in Dubai O56338O268 Dubai Call girls
Radiant Call girls in Dubai O56338O268 Dubai Call girls
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
Gram Darshan PPT cyber rural in villages of india
Gram Darshan PPT cyber rural  in villages of indiaGram Darshan PPT cyber rural  in villages of india
Gram Darshan PPT cyber rural in villages of india
 
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With RoomVIP Kolkata Call Girl Salt Lake 👉 8250192130  Available With Room
VIP Kolkata Call Girl Salt Lake 👉 8250192130 Available With Room
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service ThaneRussian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
Russian Call Girls Thane Swara 8617697112 Independent Escort Service Thane
 
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Ashram Chowk Delhi 💯Call Us 🔝8264348440🔝
 
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
FULL ENJOY Call Girls In Mayur Vihar Delhi Contact Us 8377087607
 
Russian Call girls in Dubai +971563133746 Dubai Call girls
Russian  Call girls in Dubai +971563133746 Dubai  Call girlsRussian  Call girls in Dubai +971563133746 Dubai  Call girls
Russian Call girls in Dubai +971563133746 Dubai Call girls
 
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With RoomVIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
VIP Kolkata Call Girls Salt Lake 8250192130 Available With Room
 
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Sukhdev Vihar Delhi 💯Call Us 🔝8264348440🔝
 
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With RoomVIP Kolkata Call Girl Dum Dum 👉 8250192130  Available With Room
VIP Kolkata Call Girl Dum Dum 👉 8250192130 Available With Room
 
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130  Available With RoomVIP Kolkata Call Girl Alambazar 👉 8250192130  Available With Room
VIP Kolkata Call Girl Alambazar 👉 8250192130 Available With Room
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
 
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya ShirtChallengers I Told Ya Shirt
Challengers I Told Ya ShirtChallengers I Told Ya Shirt
 
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Shahpur Jat Escort Service Delhi N.C.R.
 
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
Dwarka Sector 26 Call Girls | Delhi | 9999965857 🫦 Vanshika Verma More Our Se...
 
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in  Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Jamuna Vihar Delhi reach out to us at 🔝9953056974🔝
 

Php tips and tricks by omar bin sulaiman

  • 1. Omar Bin Sulaiman PHP Tips and Tricks by
  • 2. Introduction • In previous articles we've touched on some major sections such as MySQLi, Classes and cURL. There are a lot of other tips and tricks that you should look at, and a few of them are covered below, a couple of which are transferrable between languages. Ternary Operators • A ternary operator is a different way to layout a simple if statement. Let's take a look at one and then break it down. We'll just use a simple check to see if a GET parameter is set and not empty, if it is we will default to the word John, otherwise use the value that was set as the
  • 3. GET parameter. • $name = (!empty($_GET['name'])? $_GET['name'] : 'John'); This might look complex to begin with but there are three sections to a ternary operator: The conditional !empty($_GET['name']) The true result $_GET['name'] The false result 'John' The ternary operator will return either the true or false conditional, and therefore it will be assigned to $name. If we write this as a normal if statement we'd have one of the following: if(!empty($_GET['name'])){ $name = $_GET['name']; }else{ $name = 'John'; } // Or $name = 'John'; if(!empty($_GET['name'])) { $name = $_GET['name']; } The two above and the ternary operator will all end up with $name having something assigned to it.
  • 4. Nesting • While not advisable, it is possible to nest ternary operators within each other much like you would with standard if statements, or to form a long string of if...else if...else if...else statements. • Ternary operators can be very helpful and clean up code, but don't over complicate them otherwise your code might become inundated with large amounts of complex rediculousness that will get you hunted down by a violent psychopath.
  • 5. Class Autoloading • To make use of a class file, you have to ensure that the class you're using is available within the page that you're using it. In most cases this will be as easy as you just including the PHP file that contains the class. • But what if we have a very large number of classes that we could potentially use within one or many sections of our code? Well, we could include all of them within a common header file, or only include the ones that we know that we're going to use on that page. However, we then need to remember to include new classes whenever we want to use a different one etc. • So instead of including classes that might not get used, or we might remove later, we can employ class autoloading. When we autoload classes we can write what will happen - this might be to just throw an error to let us know that we've not included the class file, or, as in most cases, we make the function include the file that we need.
  • 6. The Code • Here we'll be using the PHP function spl_autoload_register(), we'll only be interested in the first parameter, but there are two others that you can define if you wanted the function to behave differently. I'll jump straight into how to use it: • function loadMyClass($class){ echo 'We are loading class: ' . $class; include_once('classes/' . $class . '.inc.php'); echo 'Class loaded.'; } spl_autoload_register('loadMyClass'); This will pass the name of the class that we're trying to load as the first parameter of the function that we specified. As of PHP 5.3.0 we are able to use anonymous functions, basically a function thas has no name: • spl_autoload_register(function ($class){ echo 'We are loading class: ' . $class; include_once('classes/' . $class . '.inc.php'); echo 'Class loaded.'; }); • Which will work exactly the same as the method of implementation above. So now in future, we don't need to include the class files that we'll be using as this function will run if the class is undefined, include the class and then the instantiation will be called.
  • 7. Dynamic Variables • Sometimes we want to be able to dynmically define a variable, or refer to a variable based on a user's input. Let's start with this code: • $myname = 'Michael'; $yourname = 'Ben'; if($_GET['whatname'] == 'your'){ echo $yourname; }else if($_GET['whatname'] == 'my'){ echo $myname; }else{ echo 'No Name'; } To use the value of a variable to then form another variable we use double dollar signs $$ so for example we could do $who = 'myname'; then echo $$who. This will echo out the value of $myname. • Break it down like this (exemplary code, not actual working code): $who = 'myname'; $$who = $'myname'; $$who = $myname; echo $ $who; // would return 'Michael' In reference to the code above we could look to use something along the lines of the following for very dynamically defined variables: • $who = $_GET['whatname'] . 'name'; if(isset($$who)){ echo $$who; } else{ echo 'No Name'; } As with ternary operators, you should use these loosely, as in most situations there's not much need for them, but you will most probably find some crazy-ninja somewhere who uses this notation.