PHP Nubsauce Posted June 8, 2008 Share Posted June 8, 2008 Hi, I have $username = user_name $firstname = first_name $lastname = last_name addedby = $firstname' . '$lastname How can I make it so if firstname and lastname are blank, it uses username instead? Any help would be appriciated! Thanks, Nubsauce. Quote Link to comment Share on other sites More sharing options...
dezkit Posted June 8, 2008 Share Posted June 8, 2008 <?php $username = $_POST["user_name"]; $firstname = $_POST["first_name"]; $lastname = $_POST["last_name"]; $addedby = "$firstname $lastname"; if($firstname == "" && $lastname == ""){ $username = "$firstname $lastname"; } // other codez ?> Im not even good in php, but this is just a guess Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 8, 2008 Share Posted June 8, 2008 You should use the empty() function or !isset(). Double quotes sometimes are read as a single space, don't know why, but it has happened to me. <?php $username = $_POST['user_name']; $firstname = $_POST['first_name']; $lastname = $_POST['last_name']; $addedby = $firstname . $lastname; if(empty($firstname)) { $username = "Username"; //You can change this to whatever you want username to be if it's blank } else if(empty($lastname)) { $username = "Username"; //You can change this to whatever you want username to be if it's blank } // other codez ?> You didn't give enough information in your post PHP Nubsauce, so no idea how you want it to be coded. Quote Link to comment Share on other sites More sharing options...
PHP Nubsauce Posted June 8, 2008 Author Share Posted June 8, 2008 i have $username = user_name $firstname = first_name $lastname = last_name addedby = $firstname' . '$lastname Yeah, sorry i was unclear, If firstname and lastname are both blank, I'd like addedby to be username. Thanks. Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 8, 2008 Share Posted June 8, 2008 addedby = $firstname' . '$lastname The values of your variables, should be in quotes, if they are strings. Also, is addedby a variable? You don't have "$" in front of it. Quote Link to comment Share on other sites More sharing options...
PHP Nubsauce Posted June 8, 2008 Author Share Posted June 8, 2008 Ok, I was at work, forgive me, I could not copy and paste my code. $username = $_SESSION['loggedin']; $firstname = $t['first_name']; $lastname = $t['last_name']; $addedby = $firstname. " " .$lastname; This all works fine. All I'd like to do is replace "addedby" with "username" if both firstname and lastname are blank. Thanks for your patience. Quote Link to comment Share on other sites More sharing options...
Nexy Posted June 8, 2008 Share Posted June 8, 2008 <?php $username = $_SESSION['loggedin']; $firstname = $t['first_name']; $lastname = $t['last_name']; $addedby = $firstname. " " .$lastname; if(empty($firstname) && empty($lastname)) { $addedby = $_SESSION['loggedin']; } ?> Quote Link to comment Share on other sites More sharing options...
PHP Nubsauce Posted June 8, 2008 Author Share Posted June 8, 2008 Thanks alot, this worked well. Appriciated. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.