Puzzling around with Get Page ID - php

Please have a quick look at the code below:
<?php
$pagexfoot = $_GET[page_id];
?>
<?php
if ($pagexfoot == '5' OR !isset($_GET['page_id'])) {
echo 'Hello';
} else {
echo 'Bye';
}
?>
So, if the user is on index.php?page_id=5 then it will echo "Hello" and it will echo "Bye" anywhere else. Now, how do I echo "Hello" on page index.php?page_id=5 and index.php and echo "Bye" on all other pages? Who can solve this puzzle...

<?php
if(isset($_GET['page_id']) && $_GET['page_id'] != 5)
{
echo 'Bye';
}
else
{
echo 'Hello';
}
?>

<?php
if (!isset($_GET['page_id']) || $_GET['page_id'] == 5) {
echo 'Hello';
} else {
echo 'Bye';
}
We are using the || operator to check if either it isn't set or the value is 5, if so tell "Hello" and else, Bye.

$pagexfoot = $_GET[page_id];
if($pagexfoot != '5' || isset($_GET['page_id'])) { echo 'Bye'; } elseif($pagexfoot == '5') { echo 'Hello'; }
You code loks fine to me by try the above code :)

Related

If-else condition gives me errors in PHP

I have two websites, each with it's own domain name. On load I have execute a php file that is used on both sites and has the same functionality.
Here is the code of that file:
<?php
echo '1';
if ($_SESSION["template"]=="template1";)
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template1/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template1/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template1/c.php');
}
else if ($_SESSION["template"]=="template2";)
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
echo file_get_contents('http://localhost/images/template2/a.php');
else if ($_REQUEST["cmdAction"]=='B')
echo file_get_contents('http://localhost/images/template2/b.php');
else if ($_REQUEST["cmdAction"]=='C')
echo file_get_contents('http://localhost/images/template2/c.php');
}
else {
echo 'NO DATA';
}
echo '4';
?>
On each of the two sites I set a session variable but in the above code it doesn't seem to work as I expect it to.
Am i missing something?
remove semicolon from if() and else if() statement, also add brackets when you using nested if else because it makes someone to understand easier and looks better
<?php
echo '1';
if ($_SESSION["template"]=="template1")
{
echo '2';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template1/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template1/b.php');
}
else if { ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template1/c.php');
}
}
else if ($_SESSION["template"]=="template2")
{
echo '3';
if ($_REQUEST["cmdAction"]=='A')
{
echo file_get_contents('http://localhost/images/template2/a.php');
}
else if ($_REQUEST["cmdAction"]=='B')
{
echo file_get_contents('http://localhost/images/template2/b.php');
}
else if ($_REQUEST["cmdAction"]=='C')
{
echo file_get_contents('http://localhost/images/template2/c.php');
}
}
else {
echo 'NO DATA';
}
echo '4';
?>
After reviewing your code I edited my answer. The below code will do exactly the same as your code but requires alot less code.
<?php
if (isset($_SESSION['template']) && isset($_REQUEST['cmdAction'])) {
echo file_get_contents('http://localhost/images/'.$_SESSION['template'].'/'.strtolower($_REQUEST['cmdAction']).'.php');
} else {
echo 'NO DATA';
}
?>

PHP if - else - else statement

<html>
<input type='text' name='mobile phone' value='
<?php if (strpos($phone_number, '07') === 0) {
echo $phone_number;
} else {
echo $alt_phone;
}?>'
</html>
Works fine. I would like to combine the above with:
<?php if (!empty($alt_phone)) {
echo $alt_phone;
} else {
echo '07777777777';
}?>'`
I have tried ELSEIF with the new condition, and a completely separate <?php ?> section and both times I get a blank page, instead of a textbox with a telephone number in it.
I am trying to achieve this: If $phone_number is a mobile, enter this number, otherwise enter the alt_phone, unless $alt_phone is blank, then enter '07777777777'.
try
<?php
if (!empty($phone_number)) {
echo $phone_number;
}
elseif(!empty($alt_phone))
{
echo $alt_phone;
}
else{
echo '07777777777';
}
?>'`
This will do the trick
<?php
if (strpos($phone_number, '07') === 0) {
echo $phone_number;
}
else if (!empty($alt_phone)) {
echo $alt_phone;
}
else {
echo '07777777777';
}
?>

how to append numbers into string in php

I have a for loop like this in php
<?php
for($i=0;$i<=100;$i++)
{
echo $i;
}
What I need is, I want to generate roll number using this for loop like this.
SN0001
SN0002
SN0003
.....
SN0010 not SN00010
......
SN0100 not SN000100
I have tried this
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
echo $str;
}
?>
Note this (SN0010 and SN0100). I need SN0010,SN0100 not SN00010, SN000100.
What should I do for that?
You can use str_pad() function:
<?php
for($i=0;$i<=100;$i++) {
echo 'SN' . str_pad($i, 4, STR_PAD_LEFT, '0');
}
?>
Please try this code-
for($i=0;$i<=100;$i++) {
echo 'SN' .sprintf("%'.04d\n", $i);
}
for($i=0;$i<=100;$i++)
{
if($i<=9){
$zeroValue = '000';
}
elseif($i>9 && $i<=99){
$zeroValue = '00';
}
elseif($i>99 && $i<=999){
$zeroValue = '0';
}
echo 'SN'.$zeroValue.$i.'<br>';
}
<?php
for($i=0;$i<=100;$i++) {
$str='SN00'.$i;
if(strlen($str)<6)
{
$str='SN000'.$i;
}
if(strlen($str)>6)
{
$str='SN0'.$i;
}
echo $str;
}
?>

PHP substitute, "If $something = "0" then $something1 = "no""

So basically what am trying to achieve is the following.
I am trying to make it so the following script does something in this instance:
If $something == "0" then $something1 == "no"
If $something == "1" then $something1 == "yes"
else echo "Error."
That is how I would explain what Im trying to do.
This is my current code:
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
?>
I would like it to echo "no" if the result of array "something" is "0" and echo "yes" if the result of array "something" is "1".
<?php
if($array['something'] == '0'){echo 'No';}
elseif($array['something'] == '1'){ echo 'Yes';}
else{ echo 'Error!'; }
?>
switch case is the most elegant way to go here:
switch($array['something']) {
case 0: echo 'No';break;
case 1: echo 'Yes';break;
default: echo 'Error.';
}
<?php
if(isset($_POST['resolve'])) {
$api = "http://test.com/php/";
if(!$_POST['name']) {
echo "Please, fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
echo "<div align='center'>";
if($array['something'] == '0') {
echo 'No';
}
elseif($array['something'] == '1') {
echo 'Yes';
}
else {
echo 'Error.';
}
echo "</div>";
}
}
?>
Don't forget to also do a security input check on $_POST['name']
Voila
echo $array['something1'] ? "Yes" : "No";
This sets $array['something1'] to either 'yes' or 'no' depending on the value of $array['something'].
<?php
if(isset($_POST['resolve'])){
$api = "http://test.com/php/";
if(strlen($_POST['name'])==0){
echo "fill in all fields!";
} else {
$response = file_get_contents($api.$_POST['name']);
$array = unserialize($response);
$array['something1'] = $array['something'] == 0 ? 'no' : 'yes';
?>
<div align="center"><?php echo "".$array['something1']; ?></div>
<?php
}
}
$yesno = ['No', 'Yes'];
$something1 = $yesno[$something];
That is the simplest way I know of doing it.

Include php and html inside an echo in PHP

Simple php really, just can't get it for some reason...
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits
<?php echo 'Test'; ?> and an if statement
<?php if ( $anothervalue = "test" ) { echo 'Print this'; } else
{ echo 'Print that' ; } ?>
And then some more content
<br />
Test
?>
} else {
echo 'The value didnt match';
}
?>
I need to include the php inside the echo, how can i do this?
As far as I know, you can't. echo is just for outputting.
Refactor instead.
<?php
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
?>
This is some test content, within which is some more php bits <?php echo 'Test'; ?> and an if statement
<?php
if ( $anothervalue = "test" ) {
echo 'Print this';
} else {
echo 'Print that' ;
}
?>
And then some more content
<br />
Test
<?php
} else {
echo 'The value didnt match';
}
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
I would suggest that you create a function first:
<?php
function dosomething($anothervalue){
if ( $anothervalue = "test" ) {
return 'Print this'; }
else { return 'Print that' ; }
}
then include it in your statement/text;
$myvalue = 'yes'
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits'.dosomething('anothervalue'); ?>
And then some more content
?>
You have to concatenate strings bits with the . operator:
echo 'This is some test content' . ' chained to other stuff' . someFunction() . 'and some other stuff' . $someVariable . ', right?';
try this
<?php
$myvalue = 'yes';
if ( $anothervalue == "test" ) { $test = 'Print this'; } else { $test = 'Print that' ; }
if ( $myvalue == 'yes' ) {
echo "This is some test content, within which is some more php bits $test
<br />
Test";
}
else
{
echo 'The value didnt match';
}
?>
You can certain express conditional text in the context of an echo. You can use string concatenation (using commas or dots) with inline conditional syntax (ternary operators) so that you don't have to bounce in and out of <?php nor would you need to write multiple echos.
Code: (Demo)
$myvalue = 'yes';
$anothervalue = 'test';
if ($myvalue == 'yes') {
echo 'This is some test content, within which is some more php bits Test ' , ($anothervalue == 'test' ? 'Print this' : 'Print that') , ' And then some more content<br />Test';
} else {
echo 'The value didnt match';
}
Output:
This is some test content, within which is some more php bits Test Print this And then some more content<br />Test
you can try concatenation like
echo "<img src='".$url."' />";
Try this but I suggest you go to w3schools and learn up on the basics before continuing.
<?php
$myvalue = 'yes';
if ( $myvalue == 'yes' ) {
echo 'This is some test content, within which is some more php bits Test and an if statement';
if ( $anothervalue = "test" ) {
echo 'Print this';
}
else {
echo 'Print that';
}
?>
And then some more content
<br />
Test
<?php
}
else
{
echo 'The value didnt match';
}
?>

Categories