For example if statement
if($number == 1){
echo '$number == 1';
}
Such statement need to call multiple times. And possibly latter the statement need to change.
Can place this code in external file and the use include.
But better would be create variable something like
$variable = if($number == 1){
echo '$number == 1';
}
This does not work. Tried with ( ), " ". No success.
Any ideas? Is it possible at all?
Updated question
Writing one more time for those who write that question is not understandable.
So need this code if($number == 1){ echo '$number == 1'; } (actually code is much longer) to repeat some 30 times.
And time after time some part of code manually would need to change.
One way how can I do it is to place the code in external file, for example external_file.php and then use require($_SERVER['DOCUMENT_ROOT'] . "/external_file.php");.
Instead of including external content would want to define/create (now I see that need function).
Hope now is clear what I want.
Thanks for answers, will experiment.
Please use functions for reusability
function reuseFun($num){
if($num == 1){
return 'Number = 1';
}
else{
return 'Number != 1';
}
}
echo reuseFun(1);
Maybe like this?
function checkNumber($number) {
if($number == 1){
return true;
}
}
And then you can use it like this:
if (checkNumber(1)) {
echo 'something';
}
Use function :
function myTest($number) {
if($number == 1){
echo '$number == 1';
}
}
That won't work, but you could try this:
if($number == 1) {
$variable = 'number is 1';
} elseif($number == 2) {
$variable = 'number is 2';
}
echo $variable;
It is not clear what you need with the if statement, however if you need shorthand form of if use following code.
echo 'text '. ($number == 1 ? '$number == 1' : 'something else'). ' text';
more examples
if you need to reuse the code just put this code inside the function as follows,
function print_number($number){
echo 'text '. ($number == 1 ? '$number == 1' : 'something else'). ' text';
}
Related
The problem is with this line:
if $var LIKE '1800%';
and I'm not sure how to fix it. Thanks.
<?php
//check to see if account number is like 1800*
if (isset($_POST['acct_number'])) {
$var = $_POST['acct_number'];
if $var LIKE '1800%'; {
//stop the code
exit;
} else {
echo 'normal account number';
}
}
?>
You need PHP not MySQL. For 1800% just check that it is found at position 0:
if(strpos($var, '1800') === 0) {
//stop the code
exit;
} else {
echo 'normal account number';
}
If it can occur anywhere like %1800% then:
if(strpos($var, '1800') !== false) {
//stop the code
exit;
} else {
echo 'normal account number';
}
Use substr function to get first 4 characters and compare it with 1800.
if(substr($var, 0, 4) == '1800')
{
// your code goes here.
}
``
Another way could be to use strpos()
if (strpos($var, '1800') === 0) {
// var starts with '1800'
}
I would use a regular expression for this preg_match('/^1800.+/', $search, $matches);
I have a short if else statement that I'm having trouble converting into a full one.
The reason is I would like to include some html inside instead of just text.
Auth::user()->likes()->where('status_id', $status->id)->first() ? Auth::user()->likes()->where('status_id', $status->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like'
Please note that I am doing this inside a blade template.
$likes = Auth::user()->likes()->where('status_id', $status->id)->first();
if ($likes && $likes->like == 1) {
$text = 'You like this post';
}
else {
$text = 'Like';
}
It should translate to:
if(Auth::user()->likes()->where('status_id', $status->id)->first()){
if(Auth::user()->likes()->where('status_id', $status->id)->first()->like == 1){
$var = 'You like this post';
}else{
$var = 'Like';
}
}else{
$var = 'Like';
}
Added $var to save the value, without it it doesn't make much sense.
Also, as it is, you could combine the if constructs via and
if(Auth::user()->likes()->where('status_id', $status->id)->first()
&& Auth::user()->likes()->where('status_id', $status->id)->first()->like == 1){
$if = 'You like this post';
}else{
$if = 'Like';
}
I am using the following code to pass a variable. if variable = a, do nothing.
I then want to check if variable = a, do nothing, if b, do nothing, else do something
<?
if($_GET['pageid'] == 'a'){
} else {
include('header_image.php');
}
?>
Above is the code I have working correctly for one vartiable.
How do I add an if / else?
if($_GET['pageid'] != 'a' && $_GET['pageid'] != 'b'){
//do smth
}
This is a comment - i want the formatting...
To do what you want:
if ($_GET['pageid'] == 'a') {
// do nothing for now
}
elseif ($_GET['pageid'] == 'b') {
// do some more nothing...
}
else { // we do something...
include('header_image.php');
}
You could combine the 'do nothing' tests as:
if ( $_GET['pageid'] == 'a'
|| $_GET['pageid'] == 'b') {
// do nothing for now
}
else { // we do something...
include('header_image.php');
}
I agree it reads better than the 'not equal and' tests. However, that is what 'programmers' use so it is worthwhile getting used to it.
I just started learning php and I have to do something like this which I got it working but I just have a few questions I want to ask for alternate way of doing but eh first of all the br/ is suppose to be with <> but somehow if i do that the coding at the bottom will see it as a line break.
Anyways if questions are...
With the coding below the outcome will be 0-9 (without 5) but I have to set $zero=-1 if I put $zero=0 then the outcome would be 1-9 (without 5) is there a way I don't have to make $zero=-1 and still have the outcome of 0-9 (without 5)?
I realized I have to put $zero++ before the if and continue statement if I put it at the end of the script after echo "$zero" . "br/"; the script won't run as wanted. Is this how it is suppose to be or I just don't know the other way of doing it.
Thanks in advance for people replying ^_^
$squared = pow(3,2);
echo "\"3 squared is $squared:";
echo "br/";
$zero = -1;
while ($squared > $zero)
{
$zero++;
if ($zero == 5)
{
continue;
}
else if ($squared == $zero)
{
echo "$squared\"";
}
else
{
echo "$zero" . "br/";
}
}
Here it is (you were almost there :P )
$nr = 0;
while ($squared > $nr) {
if (5 == $nr) {
$nr++; // add this
continue;
} else if ($squared == $nr) {
echo "$squared\"";
} else {
echo "$nr" . "<br/>";
}
$nr++; // move to the bottom
}
PS: You're welcome #clement
Change your while loop to while ($squared >= $zero) and then set $zero = 0;
Should work!
Good day guys,
I've made a sweet favorites function with php mysql and ajax, and its working great. Now I want to show 'favorite' when favorite = 0 and show 'unfavorite' when favorite = 1
if ($favorites == 0) {
$favorite = 'Favorite';
}
if ($favorites == 1) {
$unfavorite = 'unFavorite';
}
and echo it in the row as :
<div id="favorites">' .($favorite). ' ' .($unfavorite). '</div>
The problem is: when favorite = 0, both $favorite and $unfavorite are being shown. When favorite = 1 only $unfavorite is being shown correctly. Of course it should be $favorite OR $unfavorite. I assume the problem is clear and simple to you, please assist :)
Thanks in advance
It's easier to use just one variable:
$text = ''
if ($favorites == 0) {
$text = 'Favorite';
} else {
$text = 'unFavorite';
}
...
echo $text;
If you want to check $favorite, you are using the wrong variable in your control statement. Also, it is better coding practice to use elseif rather than if for that second if. One more thing: it's easier to manage one resulting variable.
$output = "";
if ($favorite == 0) {
$output = 'Favorite';
}
elseif ($favorite == 1) {
$output = 'unFavorite';
}
...
echo $output; // Or whatever you want to do with your output
Is $favorites an integer?
Anyway try using three equal signs (===) or else instead of the second if:
if ( $favorites === 0 )
{
// ...
}
else // or if ($favorites === 1)
{
// ...
}
You're making a toggle, so you only need one variable:
if(empty($favourites)){
$fav_toggle = 'Favorite';
} else {
$fav_toggle = 'unFavorite';
}
echo $fav_toggle;
Same code is working on me if I assigned $favorites = 0; or $favorites = 1;
You can also use if else
$favorites = 1;
if ($favorites == 0) {
$favorite = 'Favorite';
}
else if ($favorites == 1) {
$unfavorite = 'unFavorite';
}