Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:26:40+00:00 2026-05-23T16:26:40+00:00

I have this: list($firstname, $lastname) = explode(‘ ‘, $queryString); Sometiems $lastname does not gets

  • 0

I have this:

list($firstname, $lastname) = explode(' ', $queryString);

Sometiems $lastname does not gets defined, and it’s there i am getting undefined offset error.

Because it can not find anything to put in $lastname, i guess.

After the explode() i have:

if(!$lastname) { $lastname = $firstname; }

So my question is how can i define it as the $firstname if $lastname is not defined (if you wrote only ‘Adam’ and not ‘Adam Thompson’, the lastname should be defined so it is ‘Adam Adam’)

It does this for me now, but I am receiving the offset error

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:26:40+00:00Added an answer on May 23, 2026 at 4:26 pm
    list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, null);
    

    The 2 in explode() ensures, that there are at most 2 values and array_pad() ensures, that there are at least 2 values. If there is no space character , $lastname is null. This you can use to decide what comes next

    $lastname = is_null($lastname) ? $firstname : $lastname;
    

    Little update: For this specific case you can use a little trick

    list($firstname, $lastname) = array_pad(explode(' ', $queryString, 2), 2, $queryString);
    

    This will do all that in one step. It should work, because

    • There is always at least one value (for $firstname)
    • If there is one value, then $queryString == $firstname. Thats now the value that is used to fill the array up to 2 values (which is exactly one, because one value we already have)
    • If there are two values, then the array is not filled with $queryString, because we already have 2 values

    At least for readability I would prefer the first more obvious solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have contact list which contains (checkbox,firstname,lastname,phone). I am creating this list using html
I have this unordered list <ul> <li>two</li> <li>three</li> </ul> Is there a way I
def sortProfiles(p): return sorted(p, key=itemgetter('first_name')) I have a list with dictionaries. This function allows
Hello I have this list that i am working on. When i move the
i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
I have a this aspx-code: (sample) <asp:DropDownList runat=server ID=ddList1></asp:DropDownList> With this codebehind: List<System.Web.UI.WebControls.ListItem> colors
I have this code: chars = #some list try: indx = chars.index(chars) except ValueError:
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have a list of strings similar to this list: tags = ('apples', 'apricots',

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.