PHP

Code to get errors to display on a page
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

2nd code example
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);

Echo current PHP version: 8.1.32

double quotes insert db errors, the lines get cut off at the double quotes
I use the following to translate the data
$paper_name = str_replace('"', """, $paper_name);

This replaces the double quotes with the web code so it still shows up correctly

To print a single or double quote, use the backslash.
print '\'this is a test \'' ;
'this is a test '

Javascript delete button

echo "<input type=\"submit\" name=\"submit_local\" value=\"Delete\" onClick=\"javascript: return confirm('Are you sure you want to delete this record');\" />";

Simple if Statement

if (empty($x)) {
echo "x is empty no value";
} elseif (x==y){
echo "y is correct";
}else {
echo "no correct answer";
}

Check get & post isset

if(isset($_GET['id'])) {
// if exists execute
}

if(isset($_POST['id'])) {
// id exists execute
}

Comparison/logical Operators, if elseif

www.php.net/manual/en/control-structures.elseif.php
www.php.net/manual/en/language.operators.comparison.php
www.php.net/manual/en/language.operators.logical.php

include and require files

When a file is used as an include file. The php tags are requiree for PHP to recognize the PHP code
example webpath.php : code
<?php
$rootpath='/install/webroot/irwebdev';
?>

require the file in another file
require $rootpath.'/inc/topinc_css2.php';

include the file in another file
include $rootpath.'/inc/topinc_css2.php';

Normal include
include 'inc/topinc_css2.php';

Php does not regonize relative path
Use the following function to get the path and use it in the include file above
dirname(__DIR__)
example echo of function : /home/u874492000/domains/vangkin.com/public_html/test

Email check using regex

email : whocan@gmailcom

Please enter a valid email address.

Auto Generate form fields
check boxes
                       
                       

Auto Generate number between two numbers 99-105:
Random Number = "100"

Get year and create select dropdown with years

Current Year = 2025

Read from a text file

Test Text Line 1
line 2 simple
line three is more complicated
What is Line FOUR!!!!!!!

Write to a text file


Read from a text file and put data into a dropdown


Encode example

Original string : test
base64_encode : dGVzdA==
base64_decode : test

 

Hard decrypt: cVg

Hashing example

This code requires a specific version of PHP and above to work

password : abcd1234

password_hashed : $2y$10$nkrB8m5cust3LRs1ph3xruKnm50ucLj5rU8MzxqsnAaEYnIsff7JW

Password is valid!

Password confirm

Invalid password.


Array

Array = ("aa", "ai", "ia", "ee", "aw", "au", "oo", "ua")

Print random Value "a"

Print All values
0 has the value : a
1 has the value : e
2 has the value : i
3 has the value : o
4 has the value : u
5 has the value : w
6 has the value : aa
7 has the value : ai
8 has the value : ia
9 has the value : ee
10 has the value : aw
11 has the value : au
12 has the value : oo
13 has the value : ua

String to Array
$str1 = 'Brown, Nadia,<br/> Strolovitch, Dara, <br/> Affigne, Tony, <br/> Liu, Sarah, s<br/> Nunnally, <br/> Lajevardi, Nazita, <br/> Aoki, Andrew, <br/>';

Sort then print array

print 0 :

print 1 : Aoki, Andrew,

print 2 : Affigne, Tony,

print 3 : Lajevardi, Nazita,

print 4 : Liu, Sarah,

key 0 has the value :
key 1 has the value : Aoki, Andrew,
key 2 has the value : Affigne, Tony,
key 3 has the value : Lajevardi, Nazita,
key 4 has the value : Liu, Sarah,
key 5 has the value : Nunnally,
key 6 has the value : Strolovitch, Dara,
key 7 has the value : Brown, Nadia,

multidimensional array & printing a hard return in code

you must use double quotes for the hard return code to work \n

I started the multi array at index 1,1
$words = array
(1 => array(1 => "nam", "mom"),
array(1 => "txiv","dad"),
array(1 => "noj","eat"),
array(1 => "mov","food, rice"),
array(1 => "koj","you"),
array(1 => "kuv","me"),
array(1 => "npe","name")
);

1, 1 = nam
1, 2 = mom
2, 1 = txiv
3, 2 = eat
4, 1 = koj

nammomtxivdad
nojeatmovfood, rice
kojyoukuvme
npename

example of adding row to multidimensional array

Length or Array = 4

Row number 0

Ben

Row number 1

Sam

Row number 2

Rob

Row number 3

Jux


Test 2

value1=Ben, value2=22
value1=Sam, value2=15
value1=Rob, value2=5
value1=Jux, value2=17

Test 2, push

value1=Ben, value2=22
value1=Sam, value2=15
value1=Rob, value2=5
value1=Jux, value2=17
value1=Zip, value2=71
Array functions

remove dupliates : $arrat_list_new=array_unique($arrat_list); sort an array sort($array_name);

Function

Calling a function with Argument

Hello my Name is Bobby.

Calling a function with two variables

Variable one is Message1, and variable 2 is Message2.


Date Formatting

Use the PHP data function with the correct format you need it in.

$date1 = date('Y-m-d H:i:s'); echo '

Date : ' . $date1 . '

';

Current Date = 2025-08-03

Sort Date = 250803

 

Date 2 = 2025-08-03 02:59:01

Date Plus 30 days= 2025-09-02

6 months later= 2026-02-02

one year later= 2026-08-03

 

Hard Coded Date

start date 01/21/2015 (Hard coded dates must be in this format)

Plus 30 days = 2015-02-20 (format YYYY/MM/DD)

6 months later = 07-24-2015 (format MM/DD/YYYY)

1 year later = Jan 21, 2016 (format MM/DD/YYYY)

 

To compare dates use the format date("Y-m-d H:i:s"), this also works as text too

 


Get Data, generate key_id

date_default_timezone_set('America/Los_Angeles');
function rand_string( $length ) {
$chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$str='';
$size = strlen( $chars );
for( $i = 0; $i < $length; $i++ ) {
$str .= $chars[ rand( 0, $size - 1 ) ];
}
return $str;
}
$key_gen1 = strval(date('ymdHis'));
$key_gen2 = rand_string( 3 );
$KEY_ID_NEW = $key_gen1 . $key_gen2 ;
echo 'new key id, with 3 random letters : ' . $KEY_ID_NEW


year, month, day, hour, min, sec = 250803025901

new key id, with 3 random letters : 250803025901ZWW

Note you should not use special characters as the web browser will interprete the character different


Get IP

Method 1 : The REMOTE_ADDR is : 216.73.216.173

Method 2 : The get_ip is : 216.73.216.173
One Problem. If the proxy doesn't set the header, your function won't be able retrieve it. You'll need to check with your host to make sure the header is being set correctly.

bitwise & operator - find odd or evens

Array = (1 => "a", "e", "i", "o", "u", "w", "aa", "ai", "ia", "ee", "aw", "au", "oo", "ua")
Started the array index at 1

Print every even
e, o, w, ai, ee, au,

Modulus operator

Print every 3rd - reminder index starts at 0, but in this example I changed it to start at 1
i, w, ia, au,

if ($counter % 3 == 0) { echo ''; }

mod find even or odd
if (($a % 2) == 1) { echo "$a is odd." ;}
if (($a % 2) == 0) { echo "$a is even." ;}

upload a file

go to upload page

Displaying data for duplicate information in multiply rows

I created a db view so that php only needs to make one database call. However the data have the same info in multiple rows, but you only want to show it once. Use variables to gather the data during the loop and display it at the end of the db call using php.

New Line hard return must be contained in double quotes

echo "<p>test \n test2</p>";

Check for https

if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') { session_start(); } else { header( 'Location: index.php' ); }

Single and double quote issues

You can use variables in double quotes but not in single quotes
$string = 'ABC';
echo "The word is $string.";

If you use prepared statements, php will auto escape values for you. The problem is with double quotes if you pass them to another page before you pass them to mysql

View source and you can see that double quotes get cut off because of 3 double quotes are in the same value. And this is where it gets cut off

<input name="lastName" value="chao"test" type="hidden">

substitute the double quote the coded
$lastName= str_replace ( "\"", "&quot;", $_POST["lastName"] );

Reduce or limit the amount to text shown

once upon a time in a galaxy f....

$string = 'once upon a time in a galaxy far far away';
$displaytitle = substr($string, 0, 30 );
if (strlen($displaytitle) > 29) { $displaytitle = $displaytitle . '....';}
echo $displaytitle;

Next Topic

Details of the topic

Sub String - substr

echo original string : The cat is not in the hat.
echo sub string 10 : The cat is

Find String length - strlen

echo string length : 10

String positions

Text : $85 - Professionals with income between $80,000 to $99,999

First space position 3

Substring example : 85

Rest of string : Professionals with income between $80,000 to $99,999


Text : $135 - Professionals with income over $100,000

First space position 4

Substring example : 135

Rest of string : Professionals with income over $100,000


Text : $1095 - Muliply Membership combinations

First space position 5

Substring example : 1095

Rest of string : Muliply Membership combinations


Find if a string existed within another string, strpos

Line : "the|brown|fox|jumped|over|the|lazy|dog"
Notes : strpos function you must use ==== when comparing

find jumped : 14
true

find cat :
false

find " :
false

find and cut pipe strings

find 1st pipe
found at : 3, true
extract 1st word : the

find 2nd pipe
found at : 9, true
extract 2nd word : brown

find 2nd pipe
found at : 13, true
extract 3rd word : fox

finding blank lines

$string is equal to ""

find cat :
false