October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk1 min

How to Get a User ID from a Query String in PHP

For a simple PHP string, the value after the equals sign is at array index 1. For URLs with multiple parameters, parse the query and read userId by name.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For the string profile.php?userId=55, explode('=', $string, 2) puts 55 at array index 1, not index 2. If the URL may contain additional parameters, parse the query string and retrieve the named userId instead.

Get the value with explode()

When the input has the exact form profile.php?userId=55, split once at the equals sign:

$string = 'profile.php?userId=55';
$parts = explode('=', $string, 2);

echo $parts[1]; // 55

PHP arrays use zero-based indexes, so $parts[0] contains profile.php?userId and $parts[1] contains 55. There is no third element. The positive limit of 2 means the result has at most two elements; it does not make explode() interpret URL parameters. See the PHP explode() manual.

Parse the query when there may be other parameters

If the URL can include other query parameters, extract the query and parse it by parameter name. This continues to work if parameters are reordered or added:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
$url = 'profile.php?userId=55&tab=posts';
$query = parse_url($url, PHP_URL_QUERY);
$params = [];

if ($query !== null && $query !== false) {
    parse_str($query, $params);
}

$userId = $params['userId'] ?? null;

parse_url() extracts the query component; parse_str() turns its named parameters into an array. Passing $params as the second argument to parse_str() is required in PHP 8 and later. Consult the PHP parse_url() manual and PHP parse_str() manual.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Choose the approach that fits the input

Approach Best for What it returns
explode('=', $string, 2) A known, simple string with one value after the equals sign At most two pieces; the value is at index 1.
parse_url() and parse_str() A query string with named parameters, including possible additional or reordered parameters An array keyed by parameter names, such as $params['userId'].

Decide what your application should do when userId is absent; the example uses null as a fallback, but the appropriate behavior depends on the application.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.