<?php function validateStringByPrefix1(string $string, $prefix) { $valid = false; if (is_string($prefix)) { if (! empty($prefix) && strpos($string, $prefix) === 0) { $valid = true; } } elseif (is_array($prefix)) { foreach ($prefix as $partPrefix) { if (! empty($partPrefix) && strpos($string, $partPrefix) === 0) { $valid = true; break; } } } return $valid; } function validateStringByPrefix2(string $string, $prefix) { $valid = false; if (is_string($prefix)) { if (strpos($string, $prefix) === 0) { $valid = true; } } elseif (is_array($prefix)) { foreach ($prefix as $partPrefix) { if (! empty($partPrefix) && strpos($string, $partPrefix) === 0) { $valid = true; break; } } } return $valid; } $t = microtime(true); var_dump(validateStringByPrefix1("foo", "bar")); printf("1 %.6fs\n", microtime(true) - $t); $t = microtime(true); var_dump(validateStringByPrefix2("foo", "bar")); printf("2 %.6fs\n", microtime(true) - $t);
You have javascript disabled. You will not be able to edit any code.