Search
 
SCRIPT & CODE EXAMPLE
 

PHP

string match in php

//str_contains ( string $haystack , string $needle ) : bool

if (str_contains('Foo Bar Baz', 'Foo')) {
  echo 'Found';
}
Comment

php match

<?php
$food = 'cake';

$return_value = match ($food) {
    'apple' => 'This food is an apple',
    'bar' => 'This food is a bar',
    'cake' => 'This food is a cake',
};

var_dump($return_value);
?>
Comment

match php

<?php

$age = 23;

$result = match (true) {
    $age >= 65 => 'senior',
    $age >= 25 => 'adult',
    $age >= 18 => 'young adult',
    default => 'kid',
};

var_dump($result);
?>
Comment

php match

<?php
$return_value = match (subject_expression) {
    single_conditional_expression => return_expression,
    conditional_expression1, conditional_expression2 => return_expression,
};

$food = 'cake';

$return_value = match ($food) {
    'apple' => 'This food is an apple',
    'bar' => 'This food is a bar',
    'cake' => 'This food is a cake',
};

var_dump($return_value);
?>
Comment

php match expression

echo match ($x) {
    0 => 'Car',
    1 => 'Bus',
    2 => 'Bike',
};
Comment

string match in php

You could use regular expressions as its better for word matching compared to
strpos, as mentioned by other users. A strpos check for are will also return 
true for strings such as: fare, care, stare, etc. These unintended matches can 
simply be avoided in regular expression by using word boundaries.

A simple match for are could look something like this:

$a = 'How are you?';

if (preg_match('/are/', $a)) {
    echo 'true';
}
Comment

PREVIOUS NEXT
Code Example
Php :: php formData curl 
Php :: in_array php 
Php :: Call to a member function move() on string 
Php :: get current authenticated user laravel 
Php :: Group by not working - Laravel 
Php :: php example 
Php :: php add to multidimensional array 
Php :: str_shuffle in php 
Php :: Laravel Framework upgrade from older version 7.x to 8.x 
Php :: mysqli_test 
Php :: if condition in php 
Php :: wpdb get last query 
Php :: wordpress create shortcode 
Php :: array reverse php 
Php :: docker php 7.2 add ext-mongodb 
Php :: php error log 
Php :: pluck laravel 
Php :: Get all Country List using php 
Php :: laravel loop index 
Php :: add controller to laravel with requests 
Php :: wordpress admin url 
Php :: eloquent get record older than 2 days 
Php :: how to solve php mysqli_query function problem does not execute 
Php :: Get wordpress posts by category name..! 
Php :: php string interpolation 
Php :: git pull using php 
Php :: POP UP WITH PHP 
Php :: php string search in array 
Php :: php send json post 
Php :: php unique associative nested array by value 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =