Some special type of php function

Some special type of php function

A. Convert first later capital and all latter capital on string

<?php echo ucfirst($d->category); ?>
<?php echo strtoupper("Hello WORLD!"); ?>
<?php echo ucwords("hello world"); ?> ( first latter is capital of all text)
<a href="tel:8882192787">8882192787</a>

# ip adress get in laravel

$clientIP = \Request::ip();
$req->ip();

# Find index of number any string

<?php
echo strpos("I love php, I love php too!","php"); // 7
?>

strrpos() - Finds the position of the last occurrence of a string inside another string (case-sensitive)
stripos() - Finds the position of the first occurrence of a string inside another string (case-insensitive)
strripos() - Finds the position of the last occurrence of a string inside another string (case-insensitive)
To get only the last part of the path:

$actual_link = (empty($_SERVER['HTTPS']) ? 'http' : 'https') . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; basename(parse_url($url, PHP_URL_PATH));  trim(parse_url($url, PHP_URL_PATH), '/');
// Get Time From Database foreach ($sondakikaHaberler as $sonDakikaHaber){ $dt = $sonDakikaHaber->yayina_baslama_zamani; $time = $dt ->format('H:i'); $sonDakikaHaber['yayina_baslama_zamani'] = $time; }
// Get selected radio buttion value

$('input[name="name_of_your_radiobutton"]:checked').val();
B. Text Cut of string

<?php
   echo substr("Hello world",0, 6);
?>
C. Convert database content to html rendor

<?php echo stripslashes($row3['description'])?>
D. Convert small later to string

echo strtolower("Hello WORLD.");
E. Display created_at at proper format in Laravel view

{{ date('F d, Y', strtotime($img->created_at)) }}
F. Array to string conversion

$other = [a, b, c => d];
echo implode(",",$other); // string
F. String to array conversion 

$str = "Hello world. It's a beautiful day.";
print_r (explode(" ",$str)); // array
G. array_push() 

$a=array("red","green");
array_push($a,"blue","yellow");
print_r($a);
H. array_sum() 

$a=array(5,15,25);
echo array_sum($a)
H. Last inserted id 

 $query = DB::table('users')->insert([ 'first_name' => $req->customer_name, 'email' => $req->email, 'password' => Hash::make($req->password), 'type' => 'client', 'primary_admin' => 'no', 'status' => 'active', 'role_id' => 2, 'phone' => $req->customer_contact, 'clientid' => 3, 'account_owner' => 'yes', 'project_id' => $req->order_id, ]); $inserted_id = $query->id; $data = new Company;
$data->save();
$data->id;
I. csrf token inside controller

<?php echo csrf_token() ?>
J. Applied auth middleware in laravel router:

Route::get('/chats', [PusherController::class, 'chat'])->middleware('auth');
K. Display Date from database

<?php echo date('d/m/Y', strtotime($d->milestone_created)) ?>
L. Display weekly data

$meals_of_this_week = DB::table('diets')->where('user_id', $userid)->whereBetween('diet_date', [date('Y-m-d'), $sevenDays])->get();


M. Cut string

<?php
   echo substr("Hello world",1, -1); // ello worl
?>


N. print

<a href="javascript:window.print()" class="btn btn-success me-1"><i class="fa fa-print"></i></a>
O. Get age from dob in php    $dob='1993-07-01'; $dob='07 Jan 1994';         $year = (date('Y') - date('Y',strtotime($dob)));       echo $year;
P. Limit in laravel $meal1day1 = DB::table('meals')->where('user_id', $id)->where('diet_date', $DATE6)->where('day', 1)->limit(1)->get();

Leave a Reply