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;
}
?>
Related
<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';
}
?>
I want to Sum of Common Division Between two number,
in below is my code , and output is maghsom moshtarak is=24612
but Goal is 12 only;
please help me,
<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo"maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($j%$i==0)&&($h%$i==0))
{
Sum($i);
}
}
}
function Sum($i)
{
$i+=$i;
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>
<?php
$m=0;
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
Sum($i);
}
}
}
function Sum($i)
{
/* no need to use $i+=$i */
$m=$i;
echo $m;
}
SumCommon($j,$h);
?>
** Refined Code **
<?php
$j=6;
$h=12;
function SumCommon($j,$h)
{
echo "maghsom moshtarak is=";
for($i=1;$i<=$h;$i++)
{
if(($i%$j==0)&&($i%$h==0)) /* this is changed */
{
echo $i;
}
}
}
SumCommon($j,$h);
?>
How would I place the following:
<?php if (function_exists('premium_slider')){ premium_slider(1); }; ?>
Within the echo of this:
<?php if(is_page(2)){ echo ''; } ?>
Obviously I can't do this:
<?php if(is_page(2)){ echo '<?php if (function_exists('premium_slider')){ premium_slider(1); }; ?>'; } ?>
<?php
if (is_page(2) && function_exists('premium_slider')){ echo premium_slider(1); };
?>
With else:
if (is_page(2) && function_exists('premium_slider')) {
echo premium_slider(1);
} else {
echo "SO Rocks!";
}
Alternatively:
echo is_page(2) && function_exists('premium_slider') ? premium_slider(1) : 'SO Rocks!';
<?php
if (is_page(2) && function_exists('premium_slider')) {
echo premium_slider(1);
}
?>
You can use this code exactly:
<?php
if (is_page(2) && function_exists ( 'premium_slider' )) {
echo premium_slider ( 1 );
} else premium_slider ( 2 );
?>
You are using closing } at wrong place.
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 :)
I have many goals to be printed on the screen.
but it shows error when i use it like this
echo $this->validation->rshort_goal.$i;
What is the right way to use this?
if($sgoal !='')
{
$scount = count($sgoal);
$i =1;
foreach($sgoal as $row)
{
<textarea name="rshort_goal<?php print $i;?>" id="short_goal" class="short_go">
<?php if($this->validation->rshort_goal.$i)
{
echo $this->validation->rshort_goal.$i;
}
elseif($this->validation->rshort_goal.$i._error !='')
{ echo ''; }
else
{echo $$row->goal_description; }
?>
</textarea>
<?php
$i++;
}
}
echo #$this->validation->{'rshort_goal'.$i};
Perhaps you want to call a function like this?
call_user_func($this->validation, 'rshort_goal' . $i);