Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if string begins with "xx" (PHP) [duplicate]

Tags:

php

session

if ($_POST['id'] beginsWith "gm") {
$_SESSION['game']=="gmod"
}
if ($_POST['id'] beginsWith "tf2") {
$_SESSION['game']=="tf2"
}

How to do this so it will work?

like image 784
Riki137 Avatar asked Apr 29 '11 16:04

Riki137


People also ask

How do I check if a string starts with a specific character in PHP?

To check if string starts with a specific character, use PHP built-in function strpos(). Provide the string and character as arguments to strpos(), and if strpos() returns 0, then we can confirm that the string starts with the specific character, else not.

How do you check if string is a date in PHP?

In order to check if a string is a date or not, you can use the strtotime() method. Note: The strtotime() method functions by parsing an English datetime string into a UNIX timestamp.


1 Answers

You could use substring

if(substr($POST['id'],0,3) == 'tf2')
 {
  //Do something
 }

Edit: fixed incorrect function name (substring() used, should be substr())

like image 156
Matt Avatar answered Oct 14 '22 17:10

Matt